Presentation is loading. Please wait.

Presentation is loading. Please wait.

 initially Treat data as N sorted collections that are each one datum long.  merge Merge each consecutive pair of collections to form sorted collections.

Similar presentations


Presentation on theme: " initially Treat data as N sorted collections that are each one datum long.  merge Merge each consecutive pair of collections to form sorted collections."— Presentation transcript:

1  initially Treat data as N sorted collections that are each one datum long.  merge Merge each consecutive pair of collections to form sorted collections twice as large  repeat repeat the merge step over and over until a single sorted collection is formed. A more efficient sorting algorithm results from repeatedly merging small sorted lists to make larger sorted lists. The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

2 initially Treat data as N sorted collections that are each one datum long. 1st merge Example (of merge sort) 5 5 8 6 2 9 4 3 0 8629430 5 82 64 90 3 2nd merge 2 56 80 34 9 3rd merge 0 2 3 4 5 6 8 9 The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

3 What is the most efficient way to merge two sorted lists to make a larger sorted list? Example 2 56 80 34 9 The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

4 Number of merges 58629430 5 82 64 90 3 2 56 80 34 9 0 2 3 4 5 6 8 9 2 56 80 34 9 0 2 3 4 5 6 8 9 Number of Probes per merge AVERAGE Number of Probes per merge The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

5 Partitioning is done with respect to a ________, which is one value of the collection being partitioned. Another efficient sorting algorithm results from recursively partitioning collections into two smaller groups until every group is of size one (singleton groups), then concatenating all of the singleton groups. The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

6 initially Select one of the values as a pivot and partition into groups smaller and larger than pivot 1st pivot == 5 Example (of quicksort) 5 8 6 2 9 4 3 0 The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

7 2 43 08 69 2nd pivot right == 8 69 concatenate 0 2 3 4 5 6 8 9 2nd pivot left == 2 3rd pivot == 4 0 4 3 3 initially Select one of the values as a pivot and partition into groups smaller and larger than pivot 1st pivot == 5 Example (of quicksort) 5 8 6 2 9 4 3 0 The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

8 public Group quickSort(Group g) { Comparable pivot; Group small, large; if (sizeOf(g) <= 1) { return g; } else { pivot = selectedPivot(g); //partition g into small and large return concatenate( quickSort(small), pivot, quickSort(large)); } The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

9 Quicksort is more difficult to anlyze. However, in practice...  Worst Case?  Best Case? The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.


Download ppt " initially Treat data as N sorted collections that are each one datum long.  merge Merge each consecutive pair of collections to form sorted collections."

Similar presentations


Ads by Google