300 likes | 407 Views
SEQUENCES. SORTING Merge, Quick, Bucket, Heap, Radix. Divide-and-Conquer. Divide if input size < threshold, solve directly else, divide data into 2 or more subsets Recurse recursively solve subproblems associated with subsets Conquer take solutions to subproblems
E N D
SEQUENCES SORTING Merge, Quick, Bucket, Heap, Radix
Divide-and-Conquer Divide if input size < threshold, solve directly else, divide data into 2 or more subsets Recurse recursively solve subproblems associated with subsets Conquer take solutions to subproblems combine into solution to orginal problem
Merge Sort Divide divide S into 2 equal sequences S1 and S2 Recurse recursively sort sequences S1 and S2 Conquer merge sorted sequences S1 and S2 back into S
public class ListMergeSort implements SortObject { public void sort(Sequence S, Comparator c) { int n=S.size(); if (n<2) return; Sequence S1=(Sequence) S.newContainer(); for (int i=1;i<=(n+1)/2;i++) S1.insertLast(S.remove(S.first())); Sequence S2=(Sequence) S.newContainer(); for (int i=1;i<=n/2;i++) S2.insertLast(S.remove(S.first())); sort(S1,c); sort(S2,c); merge(S1,S2,c,S); } … }
public class ListMergeSort implements SortObject { … 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())) S.insertLast(S1.remove(S1.first())); else S.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())); } }
Merge Sort TC t(n) = time to merge sort n elements t(n/2) = time to merge sort n/2 elements t(n) = 2t(n/2) + O(n), a recurrence relation ==> merge sort is O(n lg n) Note: merge sort requires extra space
Recurrence Relations T(n) = aT(n/b) +cnk T(n) = O(nlogba) if a>bk T(n) = O(nklogn) if a=bk T(n) = O(nk) if a<bk
Quick Sort Divide: if S has at least 2 elements, select x from S called the pivot. Put elements of S into 3 subsequences: L, all elements in S less than x E, all elements in S equal to x G, all elements in S greater than x Recurse: recursively sort L and G Conquer: Put back elements into S in order by concatenating L, E, and G
public class ArrayQuickSort implements SortObject { public void sort(Sequence S, Comparator c) { sort(S, c, 0, S.size()-1); } private void quicksort(Sequence S, Comparator c, int leftBound, int rightBound) { if(S.size()<2) return; if(leftBound>=rightBound) return; Object pivot=S.atRank(rightBound).element(); int leftIndex=leftBound; int rightIndex=rightBound-1; … } }
public class ArrayQuickSort implements SortObject { … privat void quicksort(Sequence S, comparator c, int leftBound, int rightBound) { … while(leftIndex<=rightIndex) { while( (leftIndex<=rightIndex) && (c.isLessThanOrEqualTo(S.atRank(leftIndex).element(), pivot) ) leftIndex++; while( (rightIndex>=leftIndex) && (c.isGreaterThanOrEqualTo(S.atRank(rightIndex).element(), pivot) ) rightIndex--; if (leftIndex < rightIndex) S.swap(S.atRank(leftIndex),S.atRank(rightIndex)); } S.swap(S.atRank(leftIndex),S.atRank(rightBound)); quicksort(S, c, leftBound, leftIndex-1); quicksort(S, c, leftIndex+1, rightBound); } }
Partitioning Example partitioning around pivot 50
Quick Sort Example new pivot is selected each line
Quick Sort TC time complexity depends on pivot choice average O(n lg n) worst O(n2)
Heaps binary tree keys satisfy heap property key of every node is greater than or equal to key of its children implements priority queue
Implicit Representation of Heaps array A[0...k-1] where k is max. no. of elements heap will contain let n=current no. of elements in heap A[0...n-1] is of interest at any moment root is stored at A[0] children of any node A[i] are at A[2i+1] and A[2i+2]
Heapsort Algorithm Heapsort(A,n) Input: A (an array in the range 1 to n) Output:A (the array in sorted order) Build_Heap(A) for i n to 2 do swap (A[1],A[i]) Rearrange_Heap(i-1)
Big Omega Notation “lower bound” Let f(n) and g(n) be functions mapping integers to real numbers. Then: f(n) is Ω (g(n)) if g(n) is O(f(n)), i.e., if there exist real constants c>0 and n0>=1 such that f(n) >= cg(n) for n>=n0
Lower Bound for Sorting any comparison-based sorting algorithm requires (n log n) sorting can be seen as permutation problem determine a permutation of indices i1, i2, …, in such that a[i1]<=a[i2]<=…<=a[in] n! possible permutations comparison can eliminate up to 1/2 of search space ==> log(n!) comparisons required log(n!)= (n log n) [by Stirling’s formula]
Bucket Sort input restriction: sequence S of n items with keys in the range [0,N-1] can sort S in O(n+N) time if N is O(n), then sorting time is O(n) note: NOT based on comparison
Pseudocode Algorithm BucketSort Input: Sequence S with integer keys in range [0,N-1] Output: S sorted let B=array of N initially empty sequences for each item x in S do k=key of x remove x from S and insert at end of sequence B[k] for i 0 to N-1 do for each item x in sequence B[i] do remove x from B[i] and insert at end of S
Radix Sort generalization of bucket sort uses several passes of bucket sort (digit by digit) two versions: radix-exchange sort - “left-to-right” radix sort (most significant digit first) straight radix sort - “right-to-left” radix sort (least significant digit first)
Algorithm StraightRadixSort Input: array S of integers, each with k digits Output: S sorted //assume that all items are initially in a global queue GQ for i 0 to d-1 do // d is is number of possible digits; d=10 if decimal initialize Q[i] to be empty for i k down to 1 do while GQ is not empty do delete x from GQ d ith digit of x insert x into Q[d] for t 0 to d-1 do insert Q[t] into GQ for i 0 to n-1 do delete S[i] from GQ