Download presentation

Presentation is loading. Please wait.

Published byAlison Gaines Modified over 5 years ago

1
CS 206 Introduction to Computer Science II 04 / 27 / 2009 Instructor: Michael Eckmann

2
Michael Eckmann - Skidmore College - CS 206 - Spring 2009 Today’s Topics Questions? Comments? B-Trees (finish discussion w/ examples of insertion) quicksort

3
Each disk read will get a block which is a whole node. When we read an interior node's data from disk we get keys and links. When we read a leaf node's data from disk we get up to L data records. Insert examples –normal insert –may need to split a leaf into two option of putting a child up for adoption to a neighbor –may need to split parents may need to split the root (root will then have 2 children) Splits are infrequent (For every split there will be L/2 nonsplits, on average) Heightening of the tree is even more infrequent ( the only way the tree gets higher is when insertion leads to splitting the root.) –notice: for a tree with four levels, the root was only split 3 times during all those inserts. And for M & L as we set in the example it occurred 3 times in 10,000,000 inserts. The crux is, splits and heightening lead to more processing when they happen but they don't happen often. B-Trees

4
Let's see some insertions into an existing B-Tree –a 5-ary B-tree, M=5, (with L=5 too) will be shown on the board. First verify visually that it is indeed a 5-ary B-tree with L=5. Insert 57 and rearrange the leaf Insert 55. The leaf is full (it has L=5 items already). With the 55 it would have L+1. (L+1) / 2 is > L/2 so we can split it into 2 leaves. B-Trees

5
What would be the first step in deletion of a node? Do we have to check anything after the deletion of a node? B-Trees

6
Delete examples: –normal delete –may need to adopt if leaf goes below minimum ok if the neighbor is not already at minimum or if neighbor has minimum too, then join the two leaves together to get one full leaf –parent loses a child –if parent is now below min, then continue up if root ever loses a child and would cause only 1 remaining child, reduce the height of the tree by one and make that child the root B-Trees

7
Anyone remember MergeSort? What kind of algorithm was that? Quicksort

8
Anyone remember MergeSort? What kind of algorithm was that? –Divide and Conquer It divided the list in half and did MergeSort on each half then combined the 2 halves --- how did it do this? The divide part of MergeSort is simple. The conquer part of MergeSort is more time consuming. Quicksort

9
Quicksort is a Divide and Conquer sort algorithm as well. But instead of dividing the list into same size halves we divide it in some other way into two sublists. The DIVIDE part of Quicksort is more complex than MergeSort but the CONQUER part of Quicksort is much simpler than MergeSort. Quicksort

10
Quicksort algorithm 1) if size of list, L is 0 or 1, return 2) pick some element in list as a pivot element 3) divide the remaining elements (minus the pivot) of L into two groups, L 1, those with elements less than the pivot, and L 2, those with elements greater than or equal to the pivot 4) return (Quicksort(L 1 ) followed by pivot, followed by Quicksort(L 2 )) Depending on which is the pivot element, the sizes of the two sides could differ greatly. Compared to mergeSort, Quicksort does not guarantee equal size portions to sort (which is bad.) But, the divide stage can be done in-place (without any additional space like another array.) Quicksort

11
To pick some element in list as a pivot element we can either –pick the first (bad if list is almost sorted, why?) –pick a random one (random # generation is time consuming) –a good way is to pick the pivot is the median of 3 elements (say the median of the first, middle and last element) not much extra work the almost sorted case isn't a problem for this Divide strategy – how to divide our list into two sublists of less than pivot and greater than pivot (assume all elements distinct for now) The strategy about to be described gives good results. Quicksort

12
Divide strategy 1) swap the pivot with the last in the list 2) start index i pointing to first in list and index j to next to last element 3) while (element at i < pivot) increment i 4) while (element at j >= pivot) decrement j 5) if (i pivot and element at j is < pivot so, we swap them and repeat from step 3. 6) when i > j, we swap the pivot that is in the last place with the element at i. Quicksort

13
Notice that in the best case, if Quicksort could divide the list in equal portions at each level then we would have the fewest recursion levels O(log 2 n) The work to be done on each level is on the order of n. So in the best case quicksort is O(n log 2 n) Any ideas on what it'd be in the worst case? Quicksort

14
Let's write Quicksort –We can make quicksort be a recursive method that takes in an array the starting index of the data to be sorted the number of elements of the data to be sorted –quicksort will call a method to partition the elements find a pivot and divide a (portion of a) list into elements less than pivot, followed by pivot, followed by elements greater than pivot This method will take in –an array –the starting index of the data to be sorted –the number of elements of the data to be sorted and it will return the pivot index and alter the order of the elements of a subset of the array passed in Quicksort

15
A typical speedup for Quicksort is to do the following: –when we get down to some small number of elements (say 10) in our list, instead of using quicksort on them, we do insertion sort. –Let's use that xSort applet to visualize insertion sort. How would we alter the code we just wrote to do insertion sort when the number of elements to sort is small? Quicksort

Similar presentations

© 2020 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google