sorting, sets, and selecting

21
Sorting, Sets, and Selecting - Ed. 2. and 3.: Chapter 10 - Ed. 4.: Chapter 11

Upload: makara

Post on 06-Jan-2016

19 views

Category:

Documents


0 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Sorting, Sets, and Selecting

Sorting, Sets, and Selecting

- Ed. 2. and 3.: Chapter 10- Ed. 4.: Chapter 11

Page 2: Sorting, Sets, and Selecting

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

Page 3: Sorting, Sets, and Selecting

Merge Sort

Recall that we have learned a few sorting algorithms: Bubble sort, selection sort, and insertion sort. Now given a sequence (7, 4, 8, 2, 5, 3, 9), can we sort it with other algorithms?

Page 4: Sorting, Sets, and Selecting

Divide and Conquer The divide-and-conquer approach consists of three steps.

1. Divide: If the input size is smaller than a certain threshold (say, one or two elements), solve the problem directly using a straightforward method and return the solution. Otherwise, divide the input data into two or more disjoint subsets.

2. Recur: Recursively solve the subproblems associated with the subsets.

3. Conquer: Take the solutions to the subproblems and “merge” them into a solution to the original problem.

Page 5: Sorting, Sets, and Selecting

Using Divide-and-Conquer for Sorting Example: We have a sequence (85, 24, 63, 45, 17, 31, 96, 50). It is difficult to sort it in one step. So we split it into two sequences: (85, 24, 63, 45) and (17, 31, 96, 50) Now the problem becomes that we need sort these two sequences. However, they are smaller and hopefully easier to sort. Now we take the first sequence (85, 24, 63, 45) and see that it is still too complicated to sort in one step. So we split it further into two sequences.

Page 6: Sorting, Sets, and Selecting

(85, 24) and (63, 45) Now instead of sorting the sequence (85, 24, 63, 45), we need to sort two smaller sequences. Let us look at the first sequence (85, 24). It looks still too complicated to sort it. So we split it into two sequences again: (85) and (24) Now sorting the sequence (85, 24) becomes two smaller problems of sorting the sequences (85) and (24). Sorting the sequences (85) and (24) is trival since they are already sorted.

Page 7: Sorting, Sets, and Selecting

Now we need to merge the two sequences (85) and (24) into one sorted sequence. The result is (24, 85). Similarly, we have another sorted sequence (45, 63). After merging the sequences (24, 85) and (45, 63), we have a sequence (24, 45, 63, 85). Now we have sorted the first sequence in the first split of the original sequence (85, 24, 63, 45, 17, 31, 96, 50). Similarly for the second sequence (17, 31, 96, 50), we have the result (17, 31, 50, 95). Now merging these two sequences (24, 45, 63, 85) and (17, 31, 50, 95) yields a sorted sequence (17, 24, 31, 45, 50, 63, 85, 96).

Page 8: Sorting, Sets, and Selecting

Therefore, the merge sort involves three steps in sorting a sequenceS with n elements: 1.       Divide: If S has zero or one element, return S immediately;

it is already sorted. Otherwise, split S into twosequences S1 and S2, each containing about half of theelements in S; that is, S1 contains the first n/2 elementsof 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.

Page 9: Sorting, Sets, and Selecting

The figure here shows how the sequence is divided in the previous example.

6385 24 45 17 31 5096

6385 24 45 17 31 5096

85 24 63 45 17 31 5096

85 24 63 45 17 31 96 50

Page 10: Sorting, Sets, and Selecting

The figure here shows how the sequences are merged in the previous example.

63 8524 4517 31 50 96

63 8524 45 17 31 50 96

8524 6345 17 31 50 96

85 24 63 45 17 31 96 50

Page 11: Sorting, Sets, and Selecting

While it is easy to see how a sequence is divided into two sequences, merging two sorted sequences into one sorted sequence needs some effort. Algorithm merge(S1, S2, S) loop until either S1 or S2 is empty assign the first element in S1 to e1 assign the first element in S2 to e2 if e1 is less than or equal to e2 remove e1 from S1 and insert it into S as the last element else remove e2 from S2 and insert it into S as the last element loop until S1 is emtpy remove the first element from S1 and insert it into S as the last element loop until S2 is emtpy remove the first element from S2 and insert it into S as the last element

take the first element

comparison

Put remaining elementsin S1 into S

Put remaining elementsin S2 into S

Page 12: Sorting, Sets, and Selecting

Exam ple of m erging tw o sorted sequence

63 85

S2

45

17 31 50 96

24S1

S

Page 13: Sorting, Sets, and Selecting

63 85

S2

45

17

31 50 96

24S1

S

63 85

S2

45

17

31 50 96

24

S1

S

Page 14: Sorting, Sets, and Selecting

63 85

S2

45

17 31

50 96

24

S1

S

63 85

S2

4517 31

50 96

24

S1

S

Page 15: Sorting, Sets, and Selecting

63 85

S2

4517 31 50

96

24

S1

S

63

85

S2

4517 31 50

96

24

S1

S

Page 16: Sorting, Sets, and Selecting

63 85

S2

4517 31 50

96

24

S1

S

63 85

S2

4517 31 50 9624

S1

S

Page 17: Sorting, Sets, and Selecting

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

Page 18: Sorting, Sets, and Selecting

A Java Implementation of Merge Sort public static void mergeSort( Sequence S, Comparator c ) { int n = S.size(); if( n < 2 ) return; int i = n; Sequence S1 = new NodeSequence(); do { S1.insertLast( S.remove(S.first())); i--; } while( i > n/2 ); Sequence S2 = new NodeSequence(); do { S2.insertLast( S.remove(S.first())); i--; } while( i > 0 ); mergeSort( S1, c ); mergeSort( S2, c ); merge( S1, S2, c, S ); }

Page 19: Sorting, Sets, and Selecting

public static void merge( Sequence S1, Sequence S2, Comparator c, Sequence S ) { while( !S1.isEmpty() && !S2.isEmpty()) if(c.isLessThanOrEqualTo( S1.first().element(), S2.first().element())) S.insertLast( S1.remove( S1.first())); else S.insertLast( S2.remove( S2.first())); while( !S1.isEmpty()) S.insertLast( S1.remove( S1.first())); while( !S2.isEmpty()) S.insertLast( S2.remove( S2.first())); }

Page 20: Sorting, Sets, and Selecting

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;

}

Page 21: Sorting, Sets, and Selecting

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;

}