Sorting in Linear Time-Lecture 5 and 6

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 12

Design and Analysis of

Algorithms

Lecture: 9 & 10 Sorting in Linear Time

Engr. Simran Melwani


Counting Sort

• Counting Sort is a non-comparison-based sorting algorithm

• It is a stable sorting algorithm, meaning that it preserves the relative order of equal elements.

• Used when there are smaller integers with multiple count.

• 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)

• It is a stable sorting algorithm.

• The total space complexity is O(k + n).


Radix Sort

• 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.

• Radix Sort is a non-comparison-based sorting algorithm.

• 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

• Time Complexity: The overall time is O(d*(n + k)).

• Stable Sort: Radix Sort is stable, meaning that it preserves the order of elements with equal keys in each digit
position.

• Space Complexity: O(n+k)

• 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).

• Stable Sort: Bucket Sort is stable

• Space Complexity: O(n + k), due to the storage of buckets.

• Out-of-place (requires additional space for buckets).


Example
Thank you

You might also like