Download presentation

Presentation is loading. Please wait.

Published byMaleah Withrow Modified over 2 years ago

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

8
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 S 1 and S 2, each containing about half of the elements in S; that is, S 1 contains the first n/2 elements of S, and S 2 contains the rest. 2. Recur: Recursively sort sequence S 1 and S 2. 3. Conquer: Put back the elements into S by merging the sorted sequences S 1 and S 2 into a sorted sequence.

9
The figure here shows how the sequence is divided in the previous example. 6385244517315096 6385244517315096 8524634517315096 8524634517319650

10
The figure here shows how the sequences are merged in the previous example. 6385244517 31 5096 6385 24 4517315096 8524 63 451731 5096 8524634517319650

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

17
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.

20
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; }

21
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; } public boolean isLessThanOrEqualTo (Object v1, Object v2) { Integer u1 = (Integer) v1; Integer u2 = (Integer) v2; if (u1.intValue() == u2.intValue() or (u1.intValue() < u2.intValue()) ) return true; return false; }

Similar presentations

OK

CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.

CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on verbs for class 2 Gastrointestinal tract anatomy and physiology ppt on cells Using ppt on ipad Ppt on rbi reforms for the completion Ppt on hcl technologies ltd Ppt on acid-base titration indicator Ppt on distributed file system Ppt on essay writing skills Ppt on tv engineering Ppt on poem ozymandias by percy bysshe shelley