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

Slides:



Advertisements
Similar presentations
Chapter 9 continued: Quicksort
Advertisements

Introduction to Algorithms Quicksort
CS 206 Introduction to Computer Science II 02 / 27 / 2009 Instructor: Michael Eckmann.
David Luebke 1 4/22/2015 CS 332: Algorithms Quicksort.
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Advanced Database Discussion B Trees. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if.
CS 206 Introduction to Computer Science II 03 / 23 / 2009 Instructor: Michael Eckmann.
B+-Trees (PART 1) What is a B+ tree? Why B+ trees? Searching a B+ tree
CSCE 3110 Data Structures & Algorithm Analysis
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Using Divide and Conquer for Sorting
Quicksort CSE 331 Section 2 James Daly. Review: Merge Sort Basic idea: split the list into two parts, sort both parts, then merge the two lists
Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search.
ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
Quicksort CS 3358 Data Structures. Sorting II/ Slide 2 Introduction Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case:
CS 206 Introduction to Computer Science II 04 / 28 / 2009 Instructor: Michael Eckmann.
Data Structures Data Structures Topic #13. Today’s Agenda Sorting Algorithms: Recursive –mergesort –quicksort As we learn about each sorting algorithm,
Quicksort, Mergesort, and Heapsort. Quicksort Fastest known sorting algorithm in practice  Caveats: not stable  Vulnerable to certain attacks Average.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 4 Some of the sides are exported from different sources.
CS 171: Introduction to Computer Science II Quicksort.
Fundamentals of Algorithms MCS - 2 Lecture # 16. Quick Sort.
CS 206 Introduction to Computer Science II 12 / 09 / 2009 Instructor: Michael Eckmann.
B-Trees. Motivation for B-Trees Index structures for large datasets cannot be stored in main memory Storing it on disk requires different approach to.
CS 206 Introduction to Computer Science II 12 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 04 / 2009 Instructor: Michael Eckmann.
Quicksort. Quicksort I To sort a[left...right] : 1. if left < right: 1.1. Partition a[left...right] such that: all a[left...p-1] are less than a[p], and.
CS 206 Introduction to Computer Science II 10 / 14 / 2009 Instructor: Michael Eckmann.
Quicksort.
CS 206 Introduction to Computer Science II 12 / 03 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 12 / 01 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
Quicksort
Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 24 / 2008 Instructor: Michael Eckmann.
Sorting CS-212 Dick Steflik. Exchange Sorting Method : make n-1 passes across the data, on each pass compare adjacent items, swapping as necessary (n-1.
B-Trees. CSM B-Trees 2 Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so.
CS 206 Introduction to Computer Science II 10 / 08 / 2008 Instructor: Michael Eckmann.
Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances.
CS 206 Introduction to Computer Science II 12 / 08 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 25 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
Sorting (Part II: Divide and Conquer) CSE 373 Data Structures Lecture 14.
1 Data Structures and Algorithms Sorting. 2  Sorting is the process of arranging a list of items into a particular order  There must be some value on.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
B-Trees. CSM B-Trees 2 Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so.
COSC 2007 Data Structures II Chapter 15 External Methods.
Sorting. Pseudocode of Insertion Sort Insertion Sort To sort array A[0..n-1], sort A[0..n-2] recursively and then insert A[n-1] in its proper place among.
B-Trees. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it.
CS 206 Introduction to Computer Science II 10 / 05 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
CS 206 Introduction to Computer Science II 04 / 22 / 2009 Instructor: Michael Eckmann.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Divide And Conquer A large instance is solved as follows:  Divide the large instance into smaller instances.  Solve the smaller instances somehow. 
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
 B-tree is a specialized multiway tree designed especially for use on disk  B-Tree consists of a root node, branch nodes and leaf nodes containing the.
B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Week 15 – Friday.  What did we talk about last time?  Student questions  Review up to Exam 2  Recursion  Binary trees  Heaps  Tries  B-trees.
Quicksort This is probably the most popular sorting algorithm. It was invented by the English Scientist C.A.R. Hoare It is popular because it works well.
Sorting – Lecture 3 More about Merge Sort, Quick Sort.
ITEC 2620M Introduction to Data Structures
Sub-Quadratic Sorting Algorithms
Chapter 4.
Divide & Conquer Sorting
B-Trees.
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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