310 likes | 508 Views
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!
E N D
More Sorting Selection sort, insertion sort, bubble sort run in O( n2 ) 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! • Counting sort is based on counting, not comparing • Works well if numbers we are sorting are within a reasonably small range
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
Counting sort • A = array of numbers to sort • We assume that the numbers are between 0 and k • B = sorted array • C = temporary array
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
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
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 }
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 }
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)
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
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
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
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
Counting sort • At that point: • A = { 2, 5, 3, 0, 2, 3, 0, 3 } • B = {?, ?, ?, ?, ?, ?, 3, ? } • C = {2, 2, 4, 6, 7, 8 }
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
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
Counting sort • At that point: • A = { 2, 5, 3, 0, 2, 3, 0, 3 } • B = { ?, 0, ?, ?, ?, ?, 3, ? } • C = { 1, 2, 4, 6, 7, 8 }
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
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
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 }
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
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
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 }
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
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
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 }
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 )
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)
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
Counting sort • Stable can be used for another sorting algorithm, radix sort