Presentation is loading. Please wait.

Presentation is loading. Please wait.

Insertion Sort. Selection Sort. Bubble Sort. Heap Sort. Merge-sort. Quick-sort. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010.

Similar presentations


Presentation on theme: "Insertion Sort. Selection Sort. Bubble Sort. Heap Sort. Merge-sort. Quick-sort. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010."— Presentation transcript:

1

2 Insertion Sort. Selection Sort. Bubble Sort. Heap Sort. Merge-sort. Quick-sort. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

3 Algorithm InsertionSort(A): Input: An array A of n comparable elements Output: The array A with elements rearranged in nondecreasing order for i ← 1 to n−1 do {Insert A[i] at its proper location in A[0],A[1],...,A[i−1]} cur ← A[i] j ← i−1 while j ≥ 0 and a[j] > cur do A[j+1] ← A[j] j ← j−1 A[j+1] ← cur {cur is now in the right place} CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 3 © 2010 Goodrich, Tamassia

4 Algorithm SelectionSort(A) Input: An array A of n comparable elements Output:The array A with elements rearranged in nondecreasing order n := length[A] for i  1 to n do j  FindIndexOfSmallest( A, i, n ) swap A[i] with A[j] retrun A CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 4 Algorithm FindIndexOfSmallest( A, i, n ) smallestAt  i for j  (i+1) to n do if ( A[j] < A[smallestAt] ) smallestAt  j return smallestAt © 2010 Goodrich, Tamassia

5 Algorithm BubbleSort(A) Input: An array A of n comparable elements Output: The array A with elements rearranged in nondecreasing order for i ← 0 to N - 2 do for J ← 0 to N - 2 do if (A( J ) > A( J + 1 ) then temp ← A( J ) A( J ) ← A( J + 1 ) A( J + 1 ) ← temp return A CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 5

6 6 Divide-and conquer is a general algorithm design paradigm: Divide: divide the input data S in two or more disjoint subsets S 1, S 2, … Recur: solve the subproblems recursively Conquer: combine the solutions for S 1, S 2, …, into a solution for S The base case for the recursion are subproblems of constant size. Analysis can be done using recurrence equations. CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia

7 Divide-and conquer is a general algorithm design paradigm: Divide: divide the input data S in two disjoint subsets S 1 and S 2 Recur: solve the subproblems associated with S 1 and S 2 Conquer: combine the solutions for S 1 and S 2 into a solution for S The base case for the recursion are subproblems of size 0 or 1. Merge-sort is a sorting algorithm based on the divide- and-conquer paradigm. Like heap-sort It uses a comparator. It has O(n log n) running time. Unlike heap-sort It does not use an auxiliary priority queue It accesses data in a sequential manner (suitable to sort data on a disk) CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 7 © 2010 Goodrich, Tamassia

8 Merge-sort on an input sequence S with n elements consists of three steps: Divide: partition S into two sequences S 1 and S 2 of about n  2 elements each Recur: recursively sort S 1 and S 2 Conquer: merge S 1 and S 2 into a unique sorted sequence Algorithm mergeSort(S, C) Input: sequence S with n elements, comparator C Output: sequence S sorted according to C if S.size() > 1 (S 1, S 2 )  partition(S, n/2) mergeSort(S 1, C) mergeSort(S 2, C) S  merge(S 1, S 2 ) CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 8 © 2010 Goodrich, Tamassia

9 The conquer step of merge-sort consists of merging two sorted sequences A and B into a sorted sequence S containing the union of the elements of A and B Merging two sorted sequences, each with n  2 elements and implemented by means of a doubly linked list, takes O(n) time. Algorithm merge(A, B) Input: sequences A and B with n  2 elements each Output: sorted sequence of A  B S  empty sequence while  A.isEmpty()   B.isEmpty() if A.first().element() < B.first().element() S.addLast(A.remove(A.first())) else S.addLast(B.remove(B.first())) while  A.isEmpty() S.addLast(A.remove(A.first())) while  B.isEmpty() S.addLast(B.remove(B.first())) return S CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 9 © 2010 Goodrich, Tamassia

10 An execution of merge-sort is depicted by a binary tree each node represents a recursive call of merge-sort and stores unsorted sequence before the execution and its partition sorted sequence at the end of the execution the root is the initial call the leaves are calls on subsequences of size 0 or 1 7 2  9 4  2 4 7 9 7  2  2 79  4  4 9 7  72  29  94  4 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 10 © 2010 Goodrich, Tamassia

11 Partition 7 2 9 4  2 4 7 93 8 6 1  1 3 8 67 2  2 79 4  4 93 8  3 86 1  1 67  72  29  94  43  38  86  61  1 7 2 9 4  3 8 6 1  1 2 3 4 6 7 8 9 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 11 © 2010 Goodrich, Tamassia

12 Recursive call, partition 7 2  9 4  2 4 7 9 3 8 6 1  1 3 8 6 7 2  2 79 4  4 93 8  3 86 1  1 67  72  29  94  43  38  86  61  1 7 2 9 4  3 8 6 1  1 2 3 4 6 7 8 9 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 12 © 2010 Goodrich, Tamassia

13 Recursive call, partition 7 2  9 4  2 4 7 93 8 6 1  1 3 8 6 7  2  2 7 9 4  4 93 8  3 86 1  1 6 7  72  29  94  43  38  86  61  1 7 2 9 4  3 8 6 1  1 2 3 4 6 7 8 9 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 13 © 2010 Goodrich, Tamassia

14 Recursive call, base case 7 2  9 4  2 4 7 93 8 6 1  1 3 8 6 7  2  2 79 4  4 93 8  3 86 1  1 6 7  77  7 2  29  94  43  38  86  61  1 7 2 9 4  3 8 6 1  1 2 3 4 6 7 8 9 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 14 © 2010 Goodrich, Tamassia

15 Recursive call, base case 7 2  9 4  2 4 7 93 8 6 1  1 3 8 6 7  2  2 79 4  4 93 8  3 86 1  1 6 7  77  72  22  29  94  43  38  86  61  1 7 2 9 4  3 8 6 1  1 2 3 4 6 7 8 9 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 15 © 2010 Goodrich, Tamassia

16 Merge 7 2  9 4  2 4 7 93 8 6 1  1 3 8 6 7  2  2 7 9 4  4 93 8  3 86 1  1 6 7  77  72  22  29  94  43  38  86  61  1 7 2 9 4  3 8 6 1  1 2 3 4 6 7 8 9 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 16 © 2010 Goodrich, Tamassia

17 Recursive call, …, base case, merge 7 2  9 4  2 4 7 93 8 6 1  1 3 8 6 7  2  2 7 9 4  4 9 3 8  3 86 1  1 6 7  77  72  22  23  38  86  61  1 7 2 9 4  3 8 6 1  1 2 3 4 6 7 8 9 9  94  4 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 17 © 2010 Goodrich, Tamassia

18 Merge 7 2  9 4  2 4 7 9 3 8 6 1  1 3 8 6 7  2  2 79 4  4 93 8  3 86 1  1 6 7  77  72  22  29  94  43  38  86  61  1 7 2 9 4  3 8 6 1  1 2 3 4 6 7 8 9 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 18 © 2010 Goodrich, Tamassia

19 Recursive call, …, merge, merge 7 2  9 4  2 4 7 9 3 8 6 1  1 3 6 8 7  2  2 79 4  4 93 8  3 86 1  1 6 7  77  72  22  29  94  43  33  38  88  86  66  61  11  1 7 2 9 4  3 8 6 1  1 2 3 4 6 7 8 9 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 19 © 2010 Goodrich, Tamassia

20 Merge 7 2  9 4  2 4 7 93 8 6 1  1 3 6 8 7  2  2 79 4  4 93 8  3 86 1  1 6 7  77  72  22  29  94  43  33  38  88  86  66  61  11  1 7 2 9 4  3 8 6 1  1 2 3 4 6 7 8 9 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 20 © 2010 Goodrich, Tamassia

21 Quick-sort is a randomized sorting algorithm based on the divide-and-conquer paradigm: Divide: pick a random element x (called pivot) and partition S into L elements less than x E elements equal x G elements greater than x Recur: sort L and G Conquer: join L, E and G x x LG E x CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 21 © 2010 Goodrich, Tamassia

22 We partition an input sequence as follows: We remove, in turn, each element y from S and We insert y into L, E or G, depending on the result of the comparison with the pivot x Each insertion and removal is at the beginning or at the end of a sequence, and hence takes O(1) time Thus, the partition step of quick- sort takes O(n) time. Algorithm partition(S, p) Input: sequence S, position p of pivot Output: subsequences L, E, G of the elements of S less than, equal to, or greater than the pivot, resp. L, E, G  empty sequences x  S.remove(p) while  S.isEmpty() y  S.remove(S.first()) if y < x L.addLast(y) else if y = x E.addLast(y) else { y > x } G.addLast(y) return L, E, G CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 22 © 2010 Goodrich, Tamassia

23 An execution of quick-sort is depicted by a binary tree Each node represents a recursive call of quick-sort and stores Unsorted sequence before the execution and its pivot Sorted sequence at the end of the execution The root is the initial call The leaves are calls on subsequences of size 0 or 1 7 4 9 6 2  2 4 6 7 9 4 2  2 47 9  7 9 2  29  9 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 23 © 2010 Goodrich, Tamassia

24 Pivot selection 7 2 9 4  2 4 7 9 2  2 7 2 9 4 3 7 6 1  1 2 3 4 6 7 8 9 3 8 6 1  1 3 8 6 3  38  8 9 4  4 9 9  94  4 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 24 © 2010 Goodrich, Tamassia

25 Partition, recursive call, pivot selection 2 4 3 1  2 4 7 9 9 4  4 9 9  94  4 7 2 9 4 3 7 6 1  1 2 3 4 6 7 8 9 3 8 6 1  1 3 8 6 3  38  8 2  2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 25 © 2010 Goodrich, Tamassia

26 Partition, recursive call, base case 2 4 3 1  2 4 7 1  11  1 9 4  4 9 9  94  4 7 2 9 4 3 7 6 1   1 2 3 4 6 7 8 9 3 8 6 1  1 3 8 6 3  38  8 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 26 © 2010 Goodrich, Tamassia

27 Recursive call, …, base case, join 3 8 6 1  1 3 8 6 3  38  8 7 2 9 4 3 7 6 1  1 2 3 4 6 7 8 9 2 4 3 1  1 2 3 4 1  11  14 3  3 4 9  94  44  4 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 27 © 2010 Goodrich, Tamassia

28 Recursive call, pivot selection 7 9 7 1  1 3 8 6 8  8 7 2 9 4 3 7 6 1  1 2 3 4 6 7 8 9 2 4 3 1  1 2 3 4 1  11  14 3  3 4 9  94  44  4 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 28 © 2010 Goodrich, Tamassia

29 Partition, …, recursive call, base case 7 9 7 1  1 3 8 6 8  8 7 2 9 4 3 7 6 1  1 2 3 4 6 7 8 9 2 4 3 1  1 2 3 4 1  11  14 3  3 4 9  94  44  4 9  99  9 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 29 © 2010 Goodrich, Tamassia

30 Join, join 7 9 7  17 7 9 8  8 7 2 9 4 3 7 6 1  1 2 3 4 6 7 7 9 2 4 3 1  1 2 3 4 1  11  14 3  3 4 9  94  44  4 9  99  9 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 30 © 2010 Goodrich, Tamassia

31 Quick-sort can be implemented to run in-place In the partition step, we use replace operations to rearrange the elements of the input sequence such that the elements less than the pivot have rank less than h the elements equal to the pivot have rank between h and k the elements greater than the pivot have rank greater than k The recursive calls consider elements with rank less than h elements with rank greater than k CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 31 © 2010 Goodrich, Tamassia

32 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 32 Algorithm inPlaceQuickSort(S,a,b): Input: An array S of distinct elements; integers a and b Output: Array S with elements originally from indices from a to b, inclusive, sorted in nondecreasing order from indices a to b if a ≥ b then return {at most one element in subrange} p ← S[b] {the pivot} l ← a {will scan rightward} r ← b−1 {will scan leftward} while l ≤ r do {find an element larger than the pivot} while l ≤ r and S[l] ≤ p do l ← l+1 {find an element smaller than the pivot} while r ≥ l and S[r] ≥ p do r ← r−1 if l < r then swap the elements at S[l] and S[r] {put the pivot into its final place} swap the elements at S[l] and S[b] {recursive calls} inPlaceQuickSort(S,a,l −1) inPlaceQuickSort(S,l +1,b) {we are done at this point, since the sorted subarrays are already consecutive} © 2010 Goodrich, Tamassia

33 AlgorithmTimeNotes selection-sort O(n2)O(n2)  in-place  slow (good for small inputs) insertion-sort O(n2)O(n2)  in-place  slow (good for small inputs) bubble-sort O(n2)O(n2)  in-place  slow (good for small inputs) quick-sort O(n log n) expected  in-place, randomized  fastest (good for large inputs) heap-sort O(n log n)  in-place  fast (good for large inputs) merge-sort O(n log n)  sequential data access  fast (good for huge inputs) CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 33 © 2010 Goodrich, Tamassia

34 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 34


Download ppt "Insertion Sort. Selection Sort. Bubble Sort. Heap Sort. Merge-sort. Quick-sort. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010."

Similar presentations


Ads by Google