Download presentation
Presentation is loading. Please wait.
Published bySteven Simmons Modified over 9 years ago
1
1 Algorithms CSCI 235, Fall 2015 Lecture 17 Linear Sorting
2
2 Sorting without Comparisons Last lecture we showed that comparison based sorting algorithms cannot run faster than (nlgn). To get better efficiency, a sorting algorithm must sort based on something other than comparisons. We will examine three such sorting algorithms: Counting Sort Radix Sort Bucket Sort
3
3 Counting Sort Assumption: Counting sort assumes that each of the n input elements is an integer in the range of 1 to k, for some integer, k. Idea: For each element, x, in the input, count how many elements have a value less than x. We use this count to place x at the correct position in the sorted array. For example: If there are 3 elements less than x, we will put x at position 4 in the sorted array.
4
4 Counting Sort Algorithm COUNTING-SORT(A, B, k){ A is input array } { B holds the sorted output } { C provides temporary storage } for i <- 1 to k do{ initialization of C } C[i] <- 0 for j <- 1 to length[A] do{ inspect each input element } C[A[j]] <- C[A[j]] + 1{ if value is i then increment C[i] } { C[i] now contains the number of elements equal to i } for i <- 2 to k do{ determine # elements <= i } C[i] <- C[i] + C[i-1]{ C is a running sum } { C[i] now contains the number of elements less than or equal to i } for j <- length[A] downto 1 do{ place each element A[j] in its } B[C[A[j]]] <- A[j]{ correct sorted position in B } C[A[j]] <- C[A[j]] - 1
5
5 Example Use counting sort to sort the following array: A = 3 6 4 1 3 4 1 4 We will work this through in class.
6
6 Analyzing Counting Sort Note: No comparisons are made during counting-sort! Also note that the values of one array are used to index another array. Running time: (we will work this out in class) First for loop: Second for loop: Third for loop: Fourth for loop: Total Running time: Is it stable? Is it in place?
7
7 Radix Sort Idea: Sort numbers according to their digits. Start with the least significant digit first. Algorithm: Radix-Sort(A, d){d = number of digits} for i <- 1 to d do Stable-Sort(A, i){Sort by given digit) This algorithm was developed by IBM to sort punched cards. Each card had 80 columns with a hole punched in 1 of 12 places. Sort by last column first. Repeat sorting column by column.
8
8 Radix Sort Example Sort the following list of numbers: (we will work this out in class) 443 124 232 431 132 123 321 411 Question: What if the digit sorter is not stable?
9
9 Proving Radix Sort works Proof by induction: 1)Assume k lower order digits are sorted. 2)Show that sorting next (k+1) digit leaves list sorted for all k+1 digits. Observation 1: If 2 digits in (k+1) position are different, then the order of the lower order digits is irrelevent. 2XX > 1XX Observation 2: If 2 digits in (k+1) position are the same, then the lower order numbers are already in the correct order. They will stay in the correct order because we are using a stable sort. XX > YY, therefore 1XX > 1YY
10
10 Running time of Radix Sort The running time of radix sort depends on which stable sorting algorithm is used to sort each column of digits. If the number of possible digits is not too large, it makes sense to use counting-sort. Each call to counting-sort takes (k+n) time. We have d calls to counting sort, so T(n) = (d(k+n)) If k = O(n) then T(n) = (dn). If d is constant, then T(n) = (n)
11
11 Bucket Sort Bucket sort is like counting-sort except it can be used when the keys are not integers. If the keys are relatively evenly distributed, we can use a general bucket sort. Idea: 1) If you have n elements in a given range, sort them into buckets that divide this range into portions of width 1/n. Do this by first creating an array of n buckets, each containing an empty list. 2) Insert each element into the list associated with the bucket whose range matches that element's value. 3) Sort each of the lists in the buckets. 4) Read elements from the lists in order back into the input array. Example: We will show an example in class.
12
12 Analyzing bucket sort Is bucket-sort stable? Is it in place? Worst case running time of Bucket sort: All numbers in the same bucket: T(n) = (n 2 ) Best case running time: 1 element per bucket: T(n) = (n) Average case: Evenly distributed keys. On average T(n) = (n) (even if sorting of lists uses an algorithm that is (n 2 ))
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.