Presentation is loading. Please wait.

Presentation is loading. Please wait.

What is the runtime of the best possible (comparison based) sorting algorithm? 1.O(log n) 2.O(n) 3.O(n log n) 4.O(n 2 ) 5.None of the above.

Similar presentations


Presentation on theme: "What is the runtime of the best possible (comparison based) sorting algorithm? 1.O(log n) 2.O(n) 3.O(n log n) 4.O(n 2 ) 5.None of the above."— Presentation transcript:

1 What is the runtime of the best possible (comparison based) sorting algorithm? 1.O(log n) 2.O(n) 3.O(n log n) 4.O(n 2 ) 5.None of the above

2 Comparison-Based Sorting is a: 1.o(n ) problem 2.  (n 2 ) problem 3.O(log n) problem 4.  (n log n) problem 5.None of the above

3

4 What are the limitations of using comparisons? What are the alternatives?

5 We know sorting is O(n log n) Proved by the existence of Merge Sort How do we how sorting is  (n log n)? Write out every possible algorithm and show that each is  (n log n)? That might take a while Better idea: Find a computation model that we can analyze

6 Any sorting algorithms is a series of decisions Start with list: i 1,i 2,i 3,…,i n Compare i j and i k –Value of j and k depend on sort –Swap if needed Compare i j’ and i k’ –Value of j’ and k’ depend on sort and previous answer –Swap if needed Continue…

7 i 1,i 2,i 3,i 4,…,i n i 1 < i 2 ? i 1,i 2,i 3,i 4,…,i n i 2 <i 3 ? Yes i 2,i 1,i 3,i 4,…,i n i 1 <i 3 ? No i 1,i 2,i 3,i 4,…,i n i 3 < i 4 i 1,i 3,i 2,i 4,…,i n i 2 <i 4 ? Yes No i 2,i 1,i 3,i 4,…,i n i 3 <i 4 ? i 2,i 3,i 1,i 4,…,i n i 2 <i 4 ? YesNo

8 A tree leaf indicates where we stopped asking questions The ordering of the leaf at which we end up must be the sorted order The depth of the leaf is determined by the number of comparisons leading to it

9 How many leaves? Every possible ordering of i 1,i 2,…,i n is a solution for some possible input

10 How many leaves must the tree have? 1.  (n 2 ) 2.  (2 n ) 3.  (n!) 4.  (n n ) 5.None of the above

11 How many leaves? Every possible ordering of i 1,i 2,…,i n is a solution for some possible input There are n! possible orderings Any decision tree has n! leaves

12

13 Best we can hope for is: Balanced tree n! leaves What is the height of a balanced tree? log 2 (number of leaves) Easy to prove from previous results

14 What is the height of the tree? 1.  (log n!) 2.  (log 2 n ) 3.  (log n 2 ) 4.  (log n) 5.None of the above

15 Our height/ runtime is log 2 n! Not useful -- how bad is this? Stirling’s Approximation: n! ~ (2  ) 1/2 n n+1/2 /e n Follows: log n! ~ c 1 + c 2 n log n - c 3 n =  (n log n)

16

17 Basic Idea: Look at the list Count the number of times each element appears in the list Reconstruct list from the count Notice: Only works on integers –Or sets that can be mapped to the integers For example: Letters (A=0, B=1, C=2, …) Never compares two elements -- previous proof is not applicable!!!

18 5 10 7 2 9 1 7 3 4 1 2 3 4 5 6 7 8 9 8 1 5 1 5 1 7 1 2 7 1 9 21 1 9 1 72 1 3 7 1 4 3 1 8 4 8 1 2 3 4 5 7 7 8 9 List to be sorted Array for all possible values

19 What is the runtime of Counting Sort? 1.O(log n) 2.O(n) 3.O(n log n) 4.O(n 2 ) 5.None of the above

20 Consider the steps: –Allocate memory for the counting array: O(1) Run through the list: O(n) Anything else? All elements of the array must initially be set to 0: O(M) –Where M is the largest element of the array Finding M: O(n) Transverse array at the end: O(M) Total: O(n+M)

21 Sorting Social Security Numbers O(n + 999,999,999)=O(n) Sorting by Birth Year O(n + 2016) = O(n) Sorting n random numbers from (1,n 2 ) O(n + n 2 ) = O(n 2 ) Sorting n random numbers O(n +  ) = O(  )

22 As implemented: We cannot carry any information with the sorted values Input: (name, birth year) pairs Question: How do we sort names by birth year using counting sort?

23 Abbott, H.1980 Bell, K.1979 Bones, S.1980 Creevy, C.1981 Diggory, C.1978 Fawcett, S.1980 Jordan, L.1978 Patil, P.1980 Wood, O.1976 1977 1978 1979 1980 1981 Wood, O.Diggory, C.Abbott, H.Creevy, C.Bell, K.Jordan, L.Bones, S.Fawcett, S.Patil, P.

24 Abbott, H.1980 Bell, K.1979 Bones, S.1980 Creevy, C.1981 Diggory, C.1978 Fawcett, S.1980 Jordan, L.1978 Patil, P.1980 Wood, O.1976 Notice: This is a stable sort.

25 Sort by digit Intuitively: Sort from most significant to least significant Then you would need to keep track of the “break” points -- irritating overhead Instead: Start with the least significant digit Sorting technique must be stable… …like counting sort

26 Radix Sort 52134 7134 20698 13672 726 40314 99134 21688 55231 84397 71692 14555 52134 7134 20698 13672 726 40314 99134 21688 55231 84397 71692 14555 52134 7134 20698 13672 726 40314 99134 21688 55231 84397 71692 14555 52134 7134 20698 13672 726 40314 99134 21688 55231 84397 71692 14555 52134 7134 20698 13672 0726 40314 99134 21688 55231 84397 71692 14555 52134 07134 20698 13672 00726 40314 99134 21688 55231 84397 71692 14555 52134 7134 20698 13672 726 40314 99134 21688 55231 84397 71692 14555

27 Assume base 10 numbers (for now) Each step: Sort n numbers Range from 0 to 9 O(n+10)

28 How many steps are required for a m digit number? 1.O(log m) 2.O(m) 3.O(m log m) 4.O(m 2 ) 5.None of the above

29 Assume base 10 numbers (for now) Each step: Sort n numbers Range from 0 to 9 O(n+10) Number of steps: One per digit: log 10 M (M = max number) O((n + 10)log 10 M)=O(n log M)

30 Sorting binary numbers: O((n + 2)log 2 n) = O(n log M) Sorting hex numbers: O((n + 16)log 16 M) = O(n log M) Sorting English words O((n + 26) log 26 M) = O(n log M)

31 Sorting binary numbers: O((n + 2)log 2 n) = O(n log M) Sorting hex numbers: O((n + 16)log 16 M) = O(n log M) Sorting English words O((n + 26) log 26 M) = O(n log M) Sorting base b numbers O((n + b) log b M) = O(n log M + b log M)

32 Use: Numbers evenly distributed over the interval (0,1) Sort by tossing in buckets: Step 1: Create n buckets Step 2: Toss items into buckets Step 3: Sort each bucket with quicksort (or other)

33 Example [0.31, 0.70, 0.96, 0.23, 0.04, 0.56, 0.92, 0.89, 0.53, 0.32] [0.0-0.1) [0.1-0.2) [0.2-0.3) [0.3-0.4) [0.4-0.5) [0.5-0.6) [0.6-0.7) [0.7-0.8) [0.8-0.9) [0.9-1.0) 0.310.700.960.230.040.560.920.890.530.32 0.04 0.23 0.31 0.32 0.53 0.56 0.70 0.89 0.92 0.96

34 Assumption: Numbers uniformly distributed There will not be too many numbers in any one bucket Sorting each bucket will be quick O(n) average runtime

35 Given a set of points on the plane, which points define the borders of the convex hull? Suppose each point was a pin sticking into the paper Suppose we surrounded them with a rubber band and let it contract Which points would it touch?

36 Sample Convex Hull Convex: We cannot draw a line that intersects the hull at more than two points.

37 Input: An (unordered) set of n points on the plane (given as coordinates) Output: A list of the points on the convex hull in counter-clockwise order Algorithms: Jarvis March: O(n 2 ) Graham’s Scan: O(n log n)

38 We now know the problem is O(n log n) Can we show that is it  (n log n)? –What other lower bounds do we know? –Can we shows that an o(n log n) CH algorithm would lead to an o(n log n) sorting algorithm?

39 How do we sort with a Convex Hull algorithm? –(Sorting) Input: A sequence of numbers i 1,i 2,…,i n –Translation: Create a CH input: (i 1,i 1 2 ), (i 2,i 2 2 ), …, (i n,i n 2 )

40 List: 4, 1, 9, 3, 2, 6, 8, 7 CH Input: (4,16), (1,1), (9,81), (3,9), (2,4), (6,36), (8,64), (7,49) Return (1,1) (2,4) (3,9) (4,16) (6,36) (7,49) (8,64) (9,81)

41 If CH takes f(n) time: –Translates sorting input to CH input O(n) –Run CH algorithm O(f(n)) –Process CH results O(n) –Runtime: O(n + f(n)) If CH = o(n log n) then we can sort in o(n log n) –We can’t sort in o(n log n) –Hence: CH is  (n log n)


Download ppt "What is the runtime of the best possible (comparison based) sorting algorithm? 1.O(log n) 2.O(n) 3.O(n log n) 4.O(n 2 ) 5.None of the above."

Similar presentations


Ads by Google