Sorting in Linear Time-Lecture 5 and 6
Sorting in Linear Time-Lecture 5 and 6
Sorting in Linear Time-Lecture 5 and 6
Algorithms
• It is a stable sorting algorithm, meaning that it preserves the relative order of equal elements.
• It works by counting the occurrences of each unique value in the input array and then using this
information to place the elements in their correct sorted positions.
Counting Sort
Performance of Counting Sort
• The for loop of lines 2–3 takes time O(k), the for loop of lines 4–5 takes time O(n), the for loop of lines 7–8 takes
time O(k), and the for loop of lines 10–12 takes time O(n). Thus, the overall time is O(n + k). In practice, we
usually use counting sort when we have k = O(n), in which case the running time is O(n)
• Radix Sort works by sorting numbers digit by digit, starting from the least significant digit (rightmost) to the most
significant digit (leftmost). It sorts each digit place using a simpler sorting technique like Counting Sort.
• For decimal digits, each column uses only 10 places. A d-digit number would then occupy a field of d columns.
• It is particularly effective when the number of digits (d) is significantly less than the number of elements (n),
Radix Sort
• The following procedure assumes that each element in then-element array A has d digits, where
digit 1 is the lowest-order digit and digit d is the highest-order digit.
Performance Of Radix Sort
• Stable Sort: Radix Sort is stable, meaning that it preserves the order of elements with equal keys in each digit
position.
• Out of Place : Radix Sort typically uses auxiliary space for sorting based on the individual digits or characters.
Bucket Sort
• Bucket sort assumes that the input is drawn from a uniform distribution and has an average-case running time of O(n).
• Like counting sort, bucket sort is fast because it assumes something about the input. Whereas counting sort assumes that
the input consists of integers in a small range, bucket sort assumes that the input is generated by a random process that
distributes elements uniformly and independently over the interval [0,1).
• Works best when the input data is uniformly distributed over a known range.
Bucket Sort
Performance Of Bucket Sort
• Time Complexity: O(n + k), where n is the number of elements and k is the number of buckets. Worst-case:
O(n²) (if all elements fall into one bucket).