Lecture - 12
Lecture - 12
3
Linear Time Sorting - Advantages
• Efficient for large input sizes: The time complexity of such algorithms is O(n), which
means that the running time increases linearly with the input size. This makes them
very efficient for large data sets.
• No comparison operations: Such algorithms do not rely on elementary comparison.
• Suitability to specific input properties: Linear-time sorting algorithms often have
specific requirements or assumptions about the input elements. For example, to
calculate a sort order, you need to know the range of input elements in advance.
• Stable sort: Many linear-time sorting algorithms, including counting and radix sort,
are inherently stable.
• Ease of use: Linear-time sorting algorithms are easier to understand and debug,
making them suitable for situations where simplicity and clarity are desired.
4
Linear Time Sorting – Disadvantages
• Constraining input requirements: Linear time sorting algorithms often have
specific requirements or assumptions about the input elements. For example,
to calculate a sort order, you need to know the range of input elements in
advance.
• Additional space requirements: Some linear time sorting algorithms, such as
counting sort, require additional space to store other arrays or data
structures.
• Lack of Versatility: Linear time sorting algorithms are specialized algorithms
designed for specific scenarios or constraints.
• Inefficient for small ranges or sparse data: Such algorithms are most efficient
when the range of input elements is small and densely distributed.
• Limited to specific data types: Such algorithms, such as counting sort, are
primarily designed to sort non-negative integers or key-value objects.
5
Counting Sort
• Counting sort is a non-comparative sorting algorithm.
• It counts the occurrence of each particular element in the input array and
uses this information to determine the correct position of each element in
the sorted output array.
• Input size n
• Range k (1-5)
6
Counting Sort
• Counting sort is a non-comparative sorting algorithm.
• It counts the occurrence of each particular element in the input array and
uses this information to determine the correct position of each element in
the sorted output array.
• Input size n 2 1 2 3 1 4 2 4
• Range k (1-5)
• Create an extra array of the range size
• Advantage linear time O (n + k)
• Disadvantage
• 2 10000 3 6 7
7
Counting Sort
8
Counting Sort
9
Counting Sort
Range 0-5
10
Counting Sort - Algorithm
// Function to sort the array using counting sort
void countingSort(int A[], int B[], int k, int n)
{
int C[k + 1];
// initialize count array with zeros
for (int i = 0; i <= k; i = i + 1)
C[i] = 0;
12
Counting Sort - Complexity
// Function to sort the array using counting sort
void countingSort(int A[], int B[], int k, int n)
{
int C[k + 1];
// initialize count array with zeros
for (int i = 0; i <= k; i = i + 1) takes O(k) time
C[i] = 0;
15
Radix Sort
16
Radix Sort – Demo (LSD–Lest Significant Digit)
• Suppose, we have an array of 7
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 (i.e. Most Significant
Digit (MSD)).
• Let the initial array be:
19
Bucket Sort - Demo
• To apply bucket sort on the input array
[0.78, 0.17, 0.39, 0.26, 0.72, 0.94, 0.21, 0.12, 0.23, 0.68]
• we follow these steps:
• Step 1: Create an array of size 10, where each slot represents a bucket.
20
Bucket Sort - Demo
• Step 2: Insert elements into the buckets from the input array based on their
range.
21
Bucket Sort - Demo
• Step 3: Sort the elements within each bucket. Use any stable sorting
algorithm to sort the elements within each bucket.
22
Bucket Sort - Demo
• Step 4: Gather the elements from each bucket and put them back into the
original array.
23
Bucket Sort - Demo
• Step 5: The original array now contains the sorted elements. Return the
sorted array.
24
Bucket Sort - Demo
25
Bucket Sort - Demo
26
Bucket Sort - Complexity