1 / 21

Sorting, Sets, and Selecting

Sorting, Sets, and Selecting. Ed. 2. and 3.: Chapter 10 Ed. 4.: Chapter 11. Sorting, Sets, and Selection Merge-sort - Divide-and-conquer strategy - Merge process and merge-sort Sets - Set ADT - Set operations Quick-sort - Simple quick-sort - In-place quick-sort. Merge Sort.

makara
Download Presentation

Sorting, Sets, and Selecting

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Sorting, Sets, and Selecting • Ed. 2. and 3.: Chapter 10 • Ed. 4.: Chapter 11

  2. Sorting, Sets, and Selection • Merge-sort • - Divide-and-conquer strategy • - Merge process and merge-sort • Sets • - Set ADT • - Set operations • Quick-sort • - Simple quick-sort • - In-place quick-sort

  3. Merge Sort

  4. Therefore, the merge sort involves three steps in sorting a sequence S with n elements: 1.       Divide: If S has zero or one element, return S immediately; it is already sorted. Otherwise, split S into two sequences S1 and S2, each containing about half of the elements in S; that is, S1 contains the first n/2 elements of S, and S2 contains the rest. 2.       Recur: Recursively sort sequence S1 and S2. 3.       Conquer: Put back the elements into S by merging the sorted sequences S1 and S2 into a sorted sequence.

  5. The figure here shows how the sequence is divided in the previous example. 85 24 63 45 17 31 96 50 85 24 63 45 17 31 96 50 85 24 63 45 17 31 96 50 85 24 63 45 17 31 96 50

  6. The figure here shows how the sequences are merged in the previous example. 17 24 45 50 63 85 96 31 24 45 63 85 17 31 50 96 63 50 96 24 85 45 17 31 85 24 63 45 17 31 96 50

  7. take the first element comparison Put remaining elements in S1 into S Put remaining elements in S2 into S

  8. The Running Time of Merge Sort The running time of merge sort is proportional to nlogn where n is the size of the sequence. running time = O(nlogn). Recall that the running time for bubble sort is proportional to the square of n. Therefore merge sort is much faster than bubble sort.

  9. public class Comparator { public int compare(Object v1, Object v2) { if (((Integer) v1).intValue() < ((Integer) v2).intValue()) return -1; else if (((Integer) v1).intValue() == ((Integer) v2).intValue()) return 0; return 1; } public boolean isLessThan (Object v1, Object v2) { Integer u1 = (Integer) v1; Integer u2 = (Integer) v2; if (u1.intValue() < u2.intValue()) return true; return false; }

  10. public boolean isEqualTo (Object v1, Object v2) { Integer u1 = (Integer) v1; Integer u2 = (Integer) v2; if (u1.intValue() == u2.intValue()) return true; return false; } public boolean isLargerThan (Object v1, Object v2) { Integer u1 = (Integer) v1; Integer u2 = (Integer) v2; if (u1.intValue() > u2.intValue()) return true; return false; } }

More Related