Lecture Topic 3.1
Lecture Topic 3.1
1) Counting Sort
Counting sort is a stable sorting technique, which is used to sort objects according to the keys
that are small numbers. It counts the number of keys whose key values are same. This sorting
technique is effective when the difference between different keys are not so big, otherwise, it
can increase the space complexity.
Input:
Output:
Example:
Algorithm
2) Radix Sort
Radix sort is a sorting technique that sorts the elements by first grouping the individual digits
of the same place value. Then, sort the elements according to their increasing/decreasing
order.
Suppose, we have an array of 8 elements. First, we will sort elements based on the value of
the unit place. Then, we will sort elements based on the value of the tenth place. This process
goes on until the last significant place.
Let the initial array be [121, 432, 564, 23, 1, 45, 788]. It is sorted according to radix sort as
shown in the figure below.
1) Find the largest element in the array, i.e. max. Let X be the number of digits in max. X is
calculated because we have to go through all the significant places of all elements.
In this array [121, 432, 564, 23, 1, 45, 788], we have the largest number 788. It has 3 digits.
Therefore, the loop should go up to hundreds place (3 times)
2) Now, go through each significant place one by one.
Use any stable sorting technique to sort the digits at each significant place. We have used
counting sort for this.
Counting Sort is a linear time sorting algorithm that sort in O(n+k) time when elements are in
range from 1 to k.
We can’t use counting sort because counting sort will take O(n 2) which is worse than
comparison-based sorting algorithms. Can we sort such an array in linear time?
Radix sort is the answer. The idea of Radix Sort is to do digit by digit sort starting from least
significant digit to most significant digit. Radix sort uses counting sort as a subroutine to sort.
1) Do following for each digit i where i varies from least significant digit to the most
significant digit.
a) Sort input array using counting sort (or any stable sort) according to the i’th digit.
Source Notes:
1. https://www.geeksforgeeks.org › counting-sort
2. https://www.programiz.com/dsa/radix-sort
3. https://www.geeksforgeeks.org/radix-sort/
Lecture Video:
1. https://youtu.be/7zuGmKfUt7s
2. https://youtu.be/nu4gDuFabIM
Online Notes:
1. https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-006-
introduction-to-algorithms-fall-2011/lecture-videos/MIT6_006F11_lec07.pdf