why sorting?mohanty/col106/resources/mergesort.pdf · merge sort algorithm divide: if s has at...

43
1 Why Sorting? “When in doubt, sort” – one of the principles of algorithm design. Sorting used as a subroutine in many of the algorithms: Searching in databases: we can do binary search on sorted data A large number of computer graphics and computational geometry problems Closest pair, element uniqueness

Upload: others

Post on 16-Jul-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Why Sorting?mohanty/col106/Resources/mergesort.pdf · Merge Sort Algorithm Divide: If S has at least two elements (nothing needs to be done if S has zero or one elements), remove

1

Why Sorting?

“When in doubt, sort” – one of the principles of algorithm design. Sorting used as a subroutine in many of the algorithms:

Searching in databases: we can do binary search on sorted dataA large number of computer graphics and computational geometry problemsClosest pair, element uniqueness

Page 2: Why Sorting?mohanty/col106/Resources/mergesort.pdf · Merge Sort Algorithm Divide: If S has at least two elements (nothing needs to be done if S has zero or one elements), remove

2

Page 3: Why Sorting?mohanty/col106/Resources/mergesort.pdf · Merge Sort Algorithm Divide: If S has at least two elements (nothing needs to be done if S has zero or one elements), remove

3

Page 4: Why Sorting?mohanty/col106/Resources/mergesort.pdf · Merge Sort Algorithm Divide: If S has at least two elements (nothing needs to be done if S has zero or one elements), remove

4

A large number of sorting algorithms are developed representing different algorithm design techniques.A lower bound for sorting Ω(n log n) is used to prove lower bounds of other problems

Why Sorting? (2)

Page 5: Why Sorting?mohanty/col106/Resources/mergesort.pdf · Merge Sort Algorithm Divide: If S has at least two elements (nothing needs to be done if S has zero or one elements), remove

5

Sorting Algorithms so far

Insertion sort, selection sortWorst-case running time Θ(n2); in-place

Heap sortWorst-case running time Θ(n log n).

Page 6: Why Sorting?mohanty/col106/Resources/mergesort.pdf · Merge Sort Algorithm Divide: If S has at least two elements (nothing needs to be done if S has zero or one elements), remove

6

Divide and Conquer

Divide-and-conquer method for algorithm design:

Divide: if the input size is too large to deal with in a straightforward manner, divide the problem into two or more disjoint subproblemsConquer: use divide and conquer recursively to solve the subproblemsCombine: take the solutions to the subproblems and “merge” these solutions into a solution for the original problem

Page 7: Why Sorting?mohanty/col106/Resources/mergesort.pdf · Merge Sort Algorithm Divide: If S has at least two elements (nothing needs to be done if S has zero or one elements), remove

7

Merge Sort AlgorithmDivide: If S has at least two elements (nothing needs to be done if S has zero or one elements), remove all the elements from S and put them into two sequences, S1 and S2 , each containing about half of the elements of S. (i.e. S1 contains the first ⎡n/2⎤ elements and S2 contains the remaining ⎣n/2⎦ elements).Conquer: Sort sequences S1 and S2 using Merge Sort.Combine: Put back the elements into S by merging the sorted sequences S1 and S2 into one sorted sequence

Page 8: Why Sorting?mohanty/col106/Resources/mergesort.pdf · Merge Sort Algorithm Divide: If S has at least two elements (nothing needs to be done if S has zero or one elements), remove

8

Merge Sort: Algorithm

Merge-Sort(A, p, r)if p < r then

q←(p+r)/2Merge-Sort(A, p, q)Merge-Sort(A, q+1, r)Merge(A, p, q, r)

Merge(A, p, q, r)Take the smallest of the two topmost elements of

sequences A[p..q] and A[q+1..r] and put into the resulting sequence. Repeat this, until both sequences are empty. Copy the resulting sequence into A[p..r].

Page 9: Why Sorting?mohanty/col106/Resources/mergesort.pdf · Merge Sort Algorithm Divide: If S has at least two elements (nothing needs to be done if S has zero or one elements), remove

9

MergeSort (Example)

Page 10: Why Sorting?mohanty/col106/Resources/mergesort.pdf · Merge Sort Algorithm Divide: If S has at least two elements (nothing needs to be done if S has zero or one elements), remove

10

MergeSort (Example)

Page 11: Why Sorting?mohanty/col106/Resources/mergesort.pdf · Merge Sort Algorithm Divide: If S has at least two elements (nothing needs to be done if S has zero or one elements), remove

11

MergeSort (Example)

Page 12: Why Sorting?mohanty/col106/Resources/mergesort.pdf · Merge Sort Algorithm Divide: If S has at least two elements (nothing needs to be done if S has zero or one elements), remove

12

MergeSort (Example)

Page 13: Why Sorting?mohanty/col106/Resources/mergesort.pdf · Merge Sort Algorithm Divide: If S has at least two elements (nothing needs to be done if S has zero or one elements), remove

13

MergeSort (Example)

Page 14: Why Sorting?mohanty/col106/Resources/mergesort.pdf · Merge Sort Algorithm Divide: If S has at least two elements (nothing needs to be done if S has zero or one elements), remove

14

MergeSort (Example)

Page 15: Why Sorting?mohanty/col106/Resources/mergesort.pdf · Merge Sort Algorithm Divide: If S has at least two elements (nothing needs to be done if S has zero or one elements), remove

15

MergeSort (Example)

Page 16: Why Sorting?mohanty/col106/Resources/mergesort.pdf · Merge Sort Algorithm Divide: If S has at least two elements (nothing needs to be done if S has zero or one elements), remove

16

MergeSort (Example)

Page 17: Why Sorting?mohanty/col106/Resources/mergesort.pdf · Merge Sort Algorithm Divide: If S has at least two elements (nothing needs to be done if S has zero or one elements), remove

17

MergeSort (Example)

Page 18: Why Sorting?mohanty/col106/Resources/mergesort.pdf · Merge Sort Algorithm Divide: If S has at least two elements (nothing needs to be done if S has zero or one elements), remove

18

MergeSort (Example)

Page 19: Why Sorting?mohanty/col106/Resources/mergesort.pdf · Merge Sort Algorithm Divide: If S has at least two elements (nothing needs to be done if S has zero or one elements), remove

19

MergeSort (Example)

Page 20: Why Sorting?mohanty/col106/Resources/mergesort.pdf · Merge Sort Algorithm Divide: If S has at least two elements (nothing needs to be done if S has zero or one elements), remove

20

MergeSort (Example)

Page 21: Why Sorting?mohanty/col106/Resources/mergesort.pdf · Merge Sort Algorithm Divide: If S has at least two elements (nothing needs to be done if S has zero or one elements), remove

21

MergeSort (Example)

Page 22: Why Sorting?mohanty/col106/Resources/mergesort.pdf · Merge Sort Algorithm Divide: If S has at least two elements (nothing needs to be done if S has zero or one elements), remove

22

MergeSort (Example)

Page 23: Why Sorting?mohanty/col106/Resources/mergesort.pdf · Merge Sort Algorithm Divide: If S has at least two elements (nothing needs to be done if S has zero or one elements), remove

23

MergeSort (Example)

Page 24: Why Sorting?mohanty/col106/Resources/mergesort.pdf · Merge Sort Algorithm Divide: If S has at least two elements (nothing needs to be done if S has zero or one elements), remove

24

MergeSort (Example)

Page 25: Why Sorting?mohanty/col106/Resources/mergesort.pdf · Merge Sort Algorithm Divide: If S has at least two elements (nothing needs to be done if S has zero or one elements), remove

25

MergeSort (Example)

Page 26: Why Sorting?mohanty/col106/Resources/mergesort.pdf · Merge Sort Algorithm Divide: If S has at least two elements (nothing needs to be done if S has zero or one elements), remove

26

MergeSort (Example)

Page 27: Why Sorting?mohanty/col106/Resources/mergesort.pdf · Merge Sort Algorithm Divide: If S has at least two elements (nothing needs to be done if S has zero or one elements), remove

27

MergeSort (Example)

Page 28: Why Sorting?mohanty/col106/Resources/mergesort.pdf · Merge Sort Algorithm Divide: If S has at least two elements (nothing needs to be done if S has zero or one elements), remove

28

MergeSort (Example)

Page 29: Why Sorting?mohanty/col106/Resources/mergesort.pdf · Merge Sort Algorithm Divide: If S has at least two elements (nothing needs to be done if S has zero or one elements), remove

29

MergeSort (Example)

Page 30: Why Sorting?mohanty/col106/Resources/mergesort.pdf · Merge Sort Algorithm Divide: If S has at least two elements (nothing needs to be done if S has zero or one elements), remove

30

MergeSort (Example)

Page 31: Why Sorting?mohanty/col106/Resources/mergesort.pdf · Merge Sort Algorithm Divide: If S has at least two elements (nothing needs to be done if S has zero or one elements), remove

31

Merging Two Sequences (cont.)

Page 32: Why Sorting?mohanty/col106/Resources/mergesort.pdf · Merge Sort Algorithm Divide: If S has at least two elements (nothing needs to be done if S has zero or one elements), remove

32

Merging Two Sequences (cont.)

Page 33: Why Sorting?mohanty/col106/Resources/mergesort.pdf · Merge Sort Algorithm Divide: If S has at least two elements (nothing needs to be done if S has zero or one elements), remove

33

Merging Two Sequences (cont.)

Page 34: Why Sorting?mohanty/col106/Resources/mergesort.pdf · Merge Sort Algorithm Divide: If S has at least two elements (nothing needs to be done if S has zero or one elements), remove

34

Merging Two Sequences (cont.)

Page 35: Why Sorting?mohanty/col106/Resources/mergesort.pdf · Merge Sort Algorithm Divide: If S has at least two elements (nothing needs to be done if S has zero or one elements), remove

35

Merging Two Sequences (cont.)

Page 36: Why Sorting?mohanty/col106/Resources/mergesort.pdf · Merge Sort Algorithm Divide: If S has at least two elements (nothing needs to be done if S has zero or one elements), remove

36

Merge Sort RevisitedTo sort n numbers

if n=1 done!recursively sort 2 lists of numbers ⎣n/2⎦ and ⎡n/2⎤elementsmerge 2 sorted lists in Θ(n) time

Strategybreak problem into similar (smaller) subproblemsrecursively solve subproblemscombine solutions to answer

Page 37: Why Sorting?mohanty/col106/Resources/mergesort.pdf · Merge Sort Algorithm Divide: If S has at least two elements (nothing needs to be done if S has zero or one elements), remove

37

Recurrences

Running times of algorithms with Recursive calls can be described using recurrencesA recurrence is an equation or inequality that describes a function in terms of its value on smaller inputs

Example: Merge Sort

(1) if 1( )

2 ( / 2) ( ) if 1n

T nT n n n

Θ =⎧= ⎨ + Θ >⎩

solving_trivial_problem if 1( )

num_pieces ( / subproblem_size_factor) dividing combining if 1n

T nT n n

=⎧= ⎨ + + >⎩

Page 38: Why Sorting?mohanty/col106/Resources/mergesort.pdf · Merge Sort Algorithm Divide: If S has at least two elements (nothing needs to be done if S has zero or one elements), remove

38

Solving Recurrences

Repeated substitution methodExpanding the recurrence by substitution and noticing patterns

Substitution methodguessing the solutionsverifying the solution by the mathematical induction

Recursion-treesMaster method

templates for different classes of recurrences

Page 39: Why Sorting?mohanty/col106/Resources/mergesort.pdf · Merge Sort Algorithm Divide: If S has at least two elements (nothing needs to be done if S has zero or one elements), remove

39

Repeated Substitution MethodLet’s find the running time of merge sort (let’s assume that n=2b, for some b).

1 if 1( )

2 ( / 2) if 1n

T nT n n n

=⎧= ⎨ + >⎩

( )( )( )

2

2

3

lg

( ) 2 / 2 substitute2 2 / 4 / 2 expand

2 ( / 4) 2 substitute2 (2 ( /8) / 4) 2 expand

2 ( /8) 3 observe the pattern( ) 2 ( / 2 )

2 ( / ) lg lg

i i

n

T n T n nT n n n

T n nT n n n

T n nT n T n in

T n n n n n n n

= += + +

= += + +

= +

= += + = +

Page 40: Why Sorting?mohanty/col106/Resources/mergesort.pdf · Merge Sort Algorithm Divide: If S has at least two elements (nothing needs to be done if S has zero or one elements), remove

40

Repeated Substitution Method

The procedure is straightforward:SubstituteExpandSubstitute Expand…Observe a pattern and write how your expression looks after the i-th substitutionFind out what the value of i (e.g., lgn) should be to get the base case of the recurrence (say T(1))Insert the value of T(1) and the expression of i into your expression

Page 41: Why Sorting?mohanty/col106/Resources/mergesort.pdf · Merge Sort Algorithm Divide: If S has at least two elements (nothing needs to be done if S has zero or one elements), remove

41

Java Implementation of Merge-Sort

Page 42: Why Sorting?mohanty/col106/Resources/mergesort.pdf · Merge Sort Algorithm Divide: If S has at least two elements (nothing needs to be done if S has zero or one elements), remove

42

Java Implementation of MergeSort (cont.)public class ListMergeSort implements SortObject {

public void sort(Sequence S, Comparator c) {int n = S.size();if (n < 2) return; //sequence with 0/1 element is sorted.// divideSequence S1 = (Sequence)S.newContainer();// put the first half of S into S1for (int i=1; i <= (n+1)/2; i++) {

S1.insertLast(S.remove(S.first()));}

Sequence S2 = (Sequence)S.newContainer();// put the second half of S into S2for (int i=1; i <= n/2; i++) {

S2.insertLast(S.remove(S.first()));}

sort(S1,c); // recursort(S2,c);merge(S1,S2,c,S); // conquer}

Page 43: Why Sorting?mohanty/col106/Resources/mergesort.pdf · Merge Sort Algorithm Divide: If S has at least two elements (nothing needs to be done if S has zero or one elements), remove

43

Java Implementation of MergeSort (cont.)

public void merge(Sequence S1, Sequence S2, Comparator c, Sequence S) {while(!S1.isEmpty() && !S2.isEmpty()) {

if(c.isLessThanOrEqualTo(S1.first().element(),S2.first().element())) { // S1’s 1st elt <= S2’s 1st eltS.insertLast(S1.remove(S1.first()));

}else { // S2’s 1st elt is the smaller oneS.insertLast(S2.remove(S2.first()));

}}if(S1.isEmpty()) {

while(!S2.isEmpty()) {S.insertLast(S2.remove(S2.first()));

}}if(S2.isEmpty()) {

while(!S1.isEmpty()) {S.insertLast(S1.remove(S1.first()));

}}}