970 likes | 999 Views
Heap Sort. Heapsort. Priority queues can be used to sort in O ( N log N ) time. First, build a binary heap of N elements in O ( N ) time perform N deleteMin operations each taking O (log N ) time, hence resulting in O ( N log N ). Problem : needs an extra array.
E N D
Heapsort Priority queues can be used to sort in O(NlogN) time. First, build a binary heap of N elements in O(N) time perform NdeleteMin operations each taking O(logN) time, hence resulting in O(NlogN). Problem: needs an extra array. A clever solution: after each deleteMin heap size shrinks by 1, use this space. But the result will be a decreasing sorted order. That is, we may use a max heap by changing the heap-order property Izmir University of Economics
Heapsort Example Max heap after buildHeap phase Max heap after first deleteMax phase Izmir University of Economics
Analysis of Heapsort Izmir University of Economics worst case analysis 2N comparisons-buildHeap N-1 deleteMax operations. Theorem: Average # of comparisons to heapsort a random permutation of N distinct items is 2NlogN-O(NloglogN). Proof: on any input, cost sequence D: d1, d2,..., dN for any D, distinct deleteMax sequences total # of heaps with cost less than M is at most # of heaps with cost M < N(logN-loglogN-4) is at most (N/16)N. So the average # of comparisons is at least 2M.
7 2 9 4 2 4 7 9 7 2 2 7 9 4 4 9 7 7 2 2 9 9 4 4 Merge Sort
Divide-and conquer is a general algorithm design paradigm: Divide: divide the input data S in two disjoint subsets S1and S2 Recur: solve the subproblems associated with S1and S2 Conquer: combine the solutions for S1and S2 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) Divide-and-Conquer
Merge-sort on an input sequence S with n elements consists of three steps: Divide: partition S into two sequences S1and S2 of about n/2 elements each Recur: recursively sort S1and S2 Conquer: merge S1and S2 into a unique sorted sequence Merge-Sort AlgorithmmergeSort(S, C) Inputsequence S with n elements, comparator C Outputsequence S sorted according to C ifS.size() > 1 (S1, S2)partition(S, n/2) mergeSort(S1, C) mergeSort(S2, C) Smerge(S1, S2)
Merging Two Sorted Sequences Algorithmmerge(A, B) Inputsequences A and B withn/2 elements each Outputsorted sequence of A B S empty sequence whileA.isEmpty() B.isEmpty() ifA.first().element()<B.first().element() S.insertLast(A.remove(A.first())) else S.insertLast(B.remove(B.first())) whileA.isEmpty() S.insertLast(A.remove(A.first())) whileB.isEmpty() S.insertLast(B.remove(B.first())) return S • 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
Merge-Sort Tree • 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 7 9 4 4 9 7 7 2 2 9 9 4 4
7 2 9 4 2 4 7 9 3 8 6 1 1 3 8 6 7 2 2 7 9 4 4 9 3 8 3 8 6 1 1 6 7 7 2 2 9 9 4 4 3 3 8 8 6 6 1 1 Execution Example • Partition 7 2 9 4 3 8 6 11 2 3 4 6 7 8 9
7 2 2 7 9 4 4 9 3 8 3 8 6 1 1 6 7 7 2 2 9 9 4 4 3 3 8 8 6 6 1 1 Execution Example (cont.) • Recursive call, partition 7 2 9 4 3 8 6 11 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9 3 8 6 1 1 3 8 6
7 7 2 2 9 9 4 4 3 3 8 8 6 6 1 1 Execution Example (cont.) • Recursive call, partition 7 2 9 4 3 8 6 11 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9 3 8 6 1 1 3 8 6 7 2 2 7 9 4 4 9 3 8 3 8 6 1 1 6
7 2 2 7 9 4 4 9 3 8 3 8 6 1 1 6 Execution Example (cont.) • Recursive call, base case 7 2 9 4 3 8 6 11 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9 3 8 6 1 1 3 8 6 77 2 2 9 9 4 4 3 3 8 8 6 6 1 1
Execution Example (cont.) • Recursive call, base case 7 2 9 4 3 8 6 11 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9 3 8 6 1 1 3 8 6 7 2 2 7 9 4 4 9 3 8 3 8 6 1 1 6 77 22 9 9 4 4 3 3 8 8 6 6 1 1
Execution Example (cont.) • Merge 7 2 9 4 3 8 6 11 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9 3 8 6 1 1 3 8 6 7 22 7 9 4 4 9 3 8 3 8 6 1 1 6 77 22 9 9 4 4 3 3 8 8 6 6 1 1
Execution Example (cont.) • Recursive call, …, base case, merge 7 2 9 4 3 8 6 11 2 3 4 6 7 8 9 7 2 9 4 2 4 7 9 3 8 6 1 1 3 8 6 7 22 7 9 4 4 9 3 8 3 8 6 1 1 6 77 22 9 9 4 4 3 3 8 8 6 6 1 1
Execution Example (cont.) • Merge 7 2 9 4 3 8 6 11 2 3 4 6 7 8 9 7 2 9 42 4 7 9 3 8 6 1 1 3 8 6 7 22 7 9 4 4 9 3 8 3 8 6 1 1 6 77 22 9 9 4 4 3 3 8 8 6 6 1 1
Execution Example (cont.) • Recursive call, …, merge, merge 7 2 9 4 3 8 6 11 2 3 4 6 7 8 9 7 2 9 42 4 7 9 3 8 6 1 1 3 6 8 7 22 7 9 4 4 9 3 8 3 8 6 1 1 6 77 22 9 9 4 4 33 88 66 11
Execution Example (cont.) • Merge 7 2 9 4 3 8 6 11 2 3 4 6 7 8 9 7 2 9 42 4 7 9 3 8 6 1 1 3 6 8 7 22 7 9 4 4 9 3 8 3 8 6 1 1 6 77 22 9 9 4 4 33 88 66 11
Mergesort: sort a collection of given integers in increasing order Example: { 4, 7, 2, 3, 0, 1, 9, 11, 5, 12, 13, 6, 15, 8, 10, 14 } sorts to { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }
Merge: given two lists of numbers in increasing order, produce a list of numbers in increasing order which contains all the numbers of the given lists.
Merge: given two lists of numbers in increasing order, produce a list of numbers in increasing order which contains all the numbers of the given lists. 3, 5, 7, 11, 16, 24 1, 2, 6, 10, 12, 16, 32, 36
Merge: given two lists of numbers in increasing order, produce a list of numbers in increasing order which contains all the numbers of the given lists. First half 3, 5, 7, 11, 16, 24 1, 2, 6, 10, 12, 16, 32, 36 Second half
Merge: given two lists of numbers in increasing order, produce a list of numbers in increasing order which contains all the numbers of the given lists. 3, 5, 7, 11, 16, 24 1, 2, 6, 10, 12, 16, 32, 36 1,
Merge: given two lists of numbers in increasing order, produce a list of numbers in increasing order which contains all the numbers of the given lists. 3, 5, 7, 11, 16, 24 1, 2, 6, 10, 12, 16, 32, 36 1, 2,
Merge: given two lists of numbers in increasing order, produce a list of numbers in increasing order which contains all the numbers of the given lists. 3, 5, 7, 11, 16, 24 1, 2, 6, 10, 12, 16, 32, 36 1, 2, 3,
Merge: given two lists of numbers in increasing order, produce a list of numbers in increasing order which contains all the numbers of the given lists. 3, 5, 7, 11, 16, 24 1, 2, 6, 10, 12, 16, 32, 36 1, 2, 3, 5,
Merge: given two lists of numbers in increasing order, produce a list of numbers in increasing order which contains all the numbers of the given lists. 3, 5, 7, 11, 16, 24 1, 2, 6, 10, 12, 16, 32, 36 1, 2, 3, 5, 6,
Merge: given two lists of numbers in increasing order, produce a list of numbers in increasing order which contains all the numbers of the given lists. 3, 5, 7, 11, 16, 24 1, 2, 6, 10, 12, 16, 32, 36 1, 2, 3, 5, 6, 7,
Merge: given two lists of numbers in increasing order, produce a list of numbers in increasing order which contains all the numbers of the given lists. 3, 5, 7, 11, 16, 24 1, 2, 6, 10, 12, 16, 32, 36 1, 2, 3, 5, 6, 7, 10,
Merge: given two lists of numbers in increasing order, produce a list of numbers in increasing order which contains all the numbers of the given lists. 3, 5, 7, 11, 16, 24 1, 2, 6, 10, 12, 16, 32, 36 1, 2, 3, 5, 6, 7, 10, 11,
Merge: given two lists of numbers in increasing order, produce a list of numbers in increasing order which contains all the numbers of the given lists. 3, 5, 7, 11, 16, 24 1, 2, 6, 10, 12, 16, 32, 36 1, 2, 3, 5, 6, 7, 10, 11, 12,
Merge: given two lists of numbers in increasing order, produce a list of numbers in increasing order which contains all the numbers of the given lists. 3, 5, 7, 11, 16, 24 1, 2, 6, 10, 12, 16, 32, 36 1, 2, 3, 5, 6, 7, 10, 11, 12, 16,
Merge: given two lists of numbers in increasing order, produce a list of numbers in increasing order which contains all the numbers of the given lists. 3, 5, 7, 11, 16, 24 1, 2, 6, 10, 12, 16, 32, 36 1, 2, 3, 5, 6, 7, 10, 11, 12, 16, 16,
Merge: given two lists of numbers in increasing order, produce a list of numbers in increasing order which contains all the numbers of the given lists. 3, 5, 7, 11, 16, 24 1, 2, 6, 10, 12, 16, 32, 36 1, 2, 3, 5, 6, 7, 10, 11, 12, 16, 16, 24,
Merge: given two lists of numbers in increasing order, produce a list of numbers in increasing order which contains all the numbers of the given lists. 3, 5, 7, 11, 16, 24 1, 2, 6, 10, 12, 16, 32, 36 1, 2, 3, 5, 6, 7, 10, 11, 12, 16, 16, 24, 32, 36
{ 4, 7, 2, 3, 0, 1, 9, 11, 5, 12, 13, 6, 15, 8, 10, 14 } { 4, 7, 2, 3, 0, 1, 9, 11 } { 5, 12, 13, 6, 15, 8, 10, 14 } First half Second half
{ 4, 7, 2, 3, 0, 1, 9, 11, 5, 12, 13, 6, 15, 8, 10, 14 } { 4, 7, 2, 3, 0, 1, 9, 11 } { 5, 12, 13, 6, 15, 8, 10, 14 } { 4, 7, 2, 3 } { 0, 1, 9, 11 } { 5, 12, 13, 6 } { 15, 8, 10, 14 }
{ 4, 7, 2, 3, 0, 1, 9, 11, 5, 12, 13, 6, 15, 8, 10, 14 } { 4, 7, 2, 3, 0, 1, 9, 11 } { 5, 12, 13, 6, 15, 8, 10, 14 } { 4, 7, 2, 3 } { 0, 1, 9, 11 } { 5, 12, 13, 6 } { 15, 8, 10, 14 } { 5, 12 } { 13, 6 } { 15, 8 } { 10, 14 } { 4, 7 } { 2, 3 } { 0, 1 } { 9, 11 }
{ 4, 7, 2, 3, 0, 1, 9, 11, 5, 12, 13, 6, 15, 8, 10, 14 } { 4, 7, 2, 3, 0, 1, 9, 11 } { 5, 12, 13, 6, 15, 8, 10, 14 } { 4, 7, 2, 3 } { 0, 1, 9, 11 } { 5, 12, 13, 6 } { 15, 8, 10, 14 } { 5, 12 } { 13, 6 } { 15, 8 } { 10, 14 } { 4, 7 } { 2, 3 } { 0, 1 } { 9, 11 } { 4, 7 } { 2, 3 } { 0, 1 } { 9, 11 } { 5, 12 } { 6, 13 } { 8, 15 } { 10, 14 }
{ 4, 7, 2, 3, 0, 1, 9, 11, 5, 12, 13, 6, 15, 8, 10, 14 } { 4, 7, 2, 3, 0, 1, 9, 11 } { 5, 12, 13, 6, 15, 8, 10, 14 } { 4, 7, 2, 3 } { 0, 1, 9, 11 } { 5, 12, 13, 6 } { 15, 8, 10, 14 } { 5, 12 } { 13, 6 } { 15, 8 } { 10, 14 } { 4, 7 } { 2, 3 } { 0, 1 } { 9, 11 } { 4, 7 } { 2, 3 } { 0, 1 } { 9, 11 } { 5, 12 } { 6, 13 } { 8, 15 } { 10, 14 } { 2, 3, 4, 7 } { 0, 1, 9, 11 } { 5, 6, 12, 13 } { 8, 10, 14, 15 }
{ 4, 7, 2, 3, 0, 1, 9, 11, 5, 12, 13, 6, 15, 8, 10, 14 } { 4, 7, 2, 3, 0, 1, 9, 11 } { 5, 12, 13, 6, 15, 8, 10, 14 } { 4, 7, 2, 3 } { 0, 1, 9, 11 } { 5, 12, 13, 6 } { 15, 8, 10, 14 } { 5, 12 } { 13, 6 } { 15, 8 } { 10, 14 } { 4, 7 } { 2, 3 } { 0, 1 } { 9, 11 } { 4, 7 } { 2, 3 } { 0, 1 } { 9, 11 } { 5, 12 } { 6, 13 } { 8, 15 } { 10, 14 } { 2, 3, 4, 7 } { 0, 1, 9, 11 } { 5, 6, 12, 13 } { 8, 10, 14, 15 } { 5, 6, 8, 10, 12, 13, 14, 15 } { 0, 1, 2, 3, 4, 7, 9, 11 }
{ 4, 7, 2, 3, 0, 1, 9, 11, 5, 12, 13, 6, 15, 8, 10, 14 } { 4, 7, 2, 3, 0, 1, 9, 11 } { 5, 12, 13, 6, 15, 8, 10, 14 } { 4, 7, 2, 3 } { 0, 1, 9, 11 } { 5, 12, 13, 6 } { 15, 8, 10, 14 } { 5, 12 } { 13, 6 } { 15, 8 } { 10, 14 } { 4, 7 } { 2, 3 } { 0, 1 } { 9, 11 } { 4, 7 } { 2, 3 } { 0, 1 } { 9, 11 } { 5, 12 } { 6, 13 } { 8, 15 } { 10, 14 } { 2, 3, 4, 7 } { 0, 1, 9, 11 } { 5, 6, 12, 13 } { 8, 10, 14, 15 } { 5, 6, 8, 10, 12, 13, 14, 15 } { 0, 1, 2, 3, 4, 7, 9, 11 } { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }
Mergesort runs in O(N logN), # of comparisons is nearly optimal, fine example of a recursive algorithm. Fundamental operation is merging of 2 sorted lists. The basic merging algorithm takes 2 input arrays A and B, an output array C. 3 counters, Actr, Bctr, and Cctr are initially set to the beginning of their respective arrays. The smaller of A[Actr] and B[Bctr] is copied to C[Cctr ] and the appropriate counters are advanced. When either list is exhausted, the rest of the other list is copied to C. Izmir University of Economics 45
Mergesort - merge Izmir University of Economics 46 The time to merge is linear, at most N-1 comparisons are required (since each comparison adds an element to C. It should also be noted that after N-1 elements are added to array C, the last element need not be compared but it simply gets copied). Mergesort algorithm is then easy to describe. If N=1 DONE else { mergesort(left half); mergesort(right half); merge(left half, right half); }
Mergesort - Implementation Izmir University of Economics 47
MSort - Implementation Izmir University of Economics
Analysis of Mergesort Izmir University of Economics • Write down recurrence relations and solve them • T(1)=1 // For N=1 • T(N)=2T(N/2)+N // assumption is N=2k
Homework Assignments 50 7.11, 7.12, 7.15, 7.17, 7.43 (7.38 in 3/e), 7.53 (7.48 in 3/e) You are requested to study and solve the exercises. Note that these are for you to practice only. You are not to deliver the results to me.