1 / 10

Design and Analysis of Algorithms Non-comparison sort (sorting in linear time)

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?.

kerem
Download Presentation

Design and Analysis of Algorithms Non-comparison sort (sorting in linear time)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Design and Analysis of AlgorithmsNon-comparison sort (sorting in linear time) HaidongXue Summer 2012, at GSU

  2. Comparison based sorting • Algorithms that determine sorted order based only on comparisons between the input elements What is the lower bound?

  3. 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

  4. 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?

  5. 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

  6. 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:

  7. 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))

  8. 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))

  9. Summary Design strategies: Divide and conquer Employ certain special data structure Tradeoff between time and space

  10. 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, … … … … … … … … … … … … … … … … … … … …

More Related