1 / 31

More Sorting

More Sorting. Selection sort, insertion sort, bubble sort run in O( n 2 ) Merge sort, quick sort, heap sort run in O( n log n ). More Sorting. All of them are based on comparison sorting Can we sort an array of integers without making any comparison?. Counting sort. Yes!

Download Presentation

More Sorting

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. More Sorting Selection sort, insertion sort, bubble sort run in O( n2 ) Merge sort, quick sort, heap sort run in O( n log n )

  2. More Sorting • All of them are based on comparison sorting • Can we sort an array of integers without making any comparison?

  3. Counting sort • Yes! • Counting sort is based on counting, not comparing • Works well if numbers we are sorting are within a reasonably small range

  4. Counting sort • If the numbers are, for example, 3, 1267, and 56780, counting sort is very inefficient • But if the numbers are confined between 0 and 10, for example, it can be very efficient

  5. Counting sort • A = array of numbers to sort • We assume that the numbers are between 0 and k • B = sorted array • C = temporary array

  6. Counting sort • Step 1: C[i] = number of elements equal to i in A • Step 2: C[i] = number of elements less than or equal to i in A • Step 3: loop through elements of A, use C to slot them in order into B

  7. Counting sort • Example: A = { 2, 5, 3, 0, 2, 3, 0, 3 } •  8 elements, all between 0 and 5  k = 5 •  C is an array of k + 1 elements

  8. Counting sort • A = { 2, 5, 3, 0, 2, 3, 0, 3 } • Step 1: C[i] = number of elements equal to i in A • There are 2 0s, 2 2s, 3 3s, and 1 5 in A • C = {2, 0, 2, 3, 0, 1 }

  9. Counting sort • A = { 2, 5, 3, 0, 2, 3, 0, 3 } • C = {2, 0, 2, 3, 0, 1 } • Step 2: C[i] = number of elements less than or equal to i in A • C = {2, 2, 4, 7, 7, 8 }

  10. Counting sort • A = { 2, 5, 3, 0, 2, 3, 0, 3 } • C = {2, 2, 4, 7, 7, 8 } • Loop through elements of A, use C to slot them at the correct index in B (sorted array)

  11. Counting sort • We loop through the elements of A in reverse order when we slot them in B • This guarantees that the algorithm is stable, i.e. we preserve the original order in case 2 elements are equal

  12. Counting sort • A = { 2, 5, 3, 0, 2, 3, 0, 3 } • C = {2, 2, 4, 7, 7, 8 } • Last element in A is 3 • C[3] = 7  there are 7 elements less than or equal to 3 in A

  13. Counting sort • C[3] = 7  there are 7 elements less than or equal to 3 in A •  Slot 3 at the 7th index, i.e. 6, in the sorted array B • B[7-1] = B[6] = 3

  14. Counting sort • We have processed one of the 7 elements of the array A that is less than or equal to 3 • Adjust C to reflect that we have one fewer such element in the array A to process • C[3] = C[3] – 1 = 7 – 1 = 6

  15. Counting sort • At that point: • A = { 2, 5, 3, 0, 2, 3, 0, 3 } • B = {?, ?, ?, ?, ?, ?, 3, ? } • C = {2, 2, 4, 6, 7, 8 }

  16. Counting sort • A = { 2, 5, 3, 0, 2, 3, 0, 3 } • C = {2, 2, 4, 6, 7, 8 } • 2nd to last element in A is 0 • C[0] = 2  there are 2 elements less than or equal to 0 in A

  17. Counting sort • C[0] = 2  there are 2 elements less than or equal to 0 in A •  slot 0 at the 2nd index, i.e. 1, in the sorted array B • B[2-1] = B[1] = 0 • Adjust C • C[0] = C[0] – 1 = 2 –1 = 1

  18. Counting sort • At that point: • A = { 2, 5, 3, 0, 2, 3, 0, 3 } • B = { ?, 0, ?, ?, ?, ?, 3, ? } • C = { 1, 2, 4, 6, 7, 8 }

  19. Counting sort • A = { 2, 5, 3, 0, 2, 3, 0, 3 } • C = { 1, 2, 4, 6, 7, 8 } • Next element in A is 3 • C[3] = 6  there are 6 elements less than or equal to 3 left to process in A at this point

  20. Counting sort • C[3] = 6  there are 6 elements less than or equal to 3 in A •  slot 3 at the 6th index, i.e. 5, in the sorted array B • B[6-1] = B[5] = 3 • Adjust C • C[3] = C[3] – 1 = 6 –1 = 5

  21. Counting sort • At that point: • A = { 2, 5, 3, 0, 2, 3, 0, 3 } • B = {?, 0, ?, 2, ?, ?, 3, 3, ? } • C = { 1, 2, 4, 5, 7, 8 }

  22. Counting sort • A = { 2, 5, 3, 0, 2, 3, 0, 3 } • C = { 1, 2, 4, 5, 7, 8 } • Next element in A is 2 • C[2] = 4  there are 4 elements less than or equal to 2 left to process in A at this point

  23. Counting sort • C[2] = 4  there are 4 elements less than or equal to 2 in A •  slot 2 at the 4th index, i.e. 3, in the sorted array B • B[4-1] = B[3] = 2 • Adjust C • C[2] = C[2] – 1 = 4 –1 = 3

  24. Counting sort • At that point: • A = { 2, 5, 3, 0, 2, 3, 0, 3 } • B = { ?, 0, ?, 2, ?, 3, 3 , 5 } • C = { 1, 2, 3, 5, 7, 8 }

  25. Counting sort • A = { 2, 5, 3, 0, 2, 3, 0, 3 } • C = { 1, 2, 3, 5, 7, 8 } • Next element in A is 0 • C[0] = 1  there is 1 element less than or equal to 0 left to process in A at this point

  26. Counting sort • C[0] = 1  there is 1 element less than or equal to 0 in A •  slot 0 at the 1st index, i.e. 0, in the sorted array B • B[1-1] = B[0] = 0 • Adjust C • C[0] = C[0] – 1 = 1 –1 = 1

  27. Counting sort • At that point: • A = { 2, 5, 3, 0, 2, 3, 0, 3 } • B = { 0, 0, ?, 2, ?, 3, 3, ? } • C = { 0, 2, 3, 5, 7, 8 }

  28. Counting sort • What is the running time of counting sort • N = # of elements in A • 1st step (simple loop through elements of A ) = O( n ) • 2nd step (simple loop through elements of V ) = O( k + 1 )

  29. Counting sort • 3rd step (simple loop through elements of A ) = O( n ) • Overall, O( n + k ) • If k = O( n ) or less  better than O( n log n )  quite good • It all depends on the value of k (the “range” of the numbers)

  30. Counting sort • Property of counting sort: it is stable • The numbers with the same value appear in the sorted array in the same order as they do in the original, non-sorted array

  31. Counting sort • Stable  can be used for another sorting algorithm, radix sort

More Related