120 likes | 324 Views
Design and Analysis of Algorithms Non-comparison sort (sorting in linear time). Haidong Xue Summer 2012, at GSU. Comparison based sorting. Algorithms that determine sorted order based only on comparisons between the input elements. What is the lower bound?.
E N D
Design and Analysis of AlgorithmsNon-comparison sort (sorting in linear time) HaidongXue Summer 2012, at GSU
Comparison based sorting • Algorithms that determine sorted order based only on comparisons between the input elements What is the lower bound?
Lower bounds for comparison based sorting For n element array, how many possible inputs are there? <? N Factorial of n ----- n! Y What is the shortest tree can have n! leaves? <? <? A perfect tree, Y …… As a result …… <? ………………………….. N Y Done Done
Sorting in linear time • Can we sort an array in linear time? • Yes, but not for free • E.g. sort cards with 13 slots • What if there are more than one elements in the same slot?
Counting Sort • Input: array A[1, … , n]; k (elements in A have values from 1 to k) • Output: sorted array A Algorithm: • Create a counter array C[1, …, k] • Create an auxiliary array B[1, …, n] • Scan A once, record element frequency in C • Calculate prefix sum in C • Scan A in the reverse order, copy each element to B at the correct position according to C. • Copy B to A
Counting Sort 6 7 8 2 1 3 4 5 2 3 A: 7 3 2 5 3 6 6 2 3 7 3 2 5 3 6 7 2 1 3 4 5 C: 0 2 0 1 1 1 3 0 2 3 5 8 5 6 7 4 5 7 1 2 6 Position indicator: 0 6 7 8 2 1 3 4 5 B:
Analysis of Counting Sort • Input: array A[1, … , n]; k (elements in A have values from 1 to k) • Output: sorted array A Algorithm: • Create a counter array C[1, …, k] • Create an auxiliary array B[1, …, n] • Scan A once, record element frequency in C • Calculate prefix sum in C • Scan A in the reverse order, copy each element to B at the correct position according to C. • Copy B to A Time Space O(k) O(n) O(n) O(k) O(n) O(n) O(n+k)=O(n) (if k=O(n)) O(n+k)=O(n) (if k=O(n))
Radix-Sort • Input: array A[1, … , n]; d (number of digit a element has) • Output: sorted array A Algorithm: for each digit{ use a stable sort to sort A on a digit } T(n)=O(d(n+k))
Summary Design strategies: Divide and conquer Employ certain special data structure Tradeoff between time and space
Knowledge tree Algorithms Classic data structure Analysis Algorithms for classic problems Design … Sorting Shortest path Matrix multiplication Asymptotic notations Probabilistic analysis Heap, Hashing, Binary Tree, RBT, …. Dynamic Programming Greedy Divide & Conquer O(), o(), (), (), () Quicksort, Heapsort, Mergesort, … … … … … … … … … … … … … … … … … … … …