You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The lower bound for Comparison based sorting algorithm (Merge Sort, Heap Sort, Quick-Sort .. etc) is Ω(nLogn), i.e., they cannot do better than nLogn.
3
+
The lower bound for Comparison based sorting algorithm (Merge Sort, Heap Sort, Quick-Sort .. etc) is `Ω(nLogn)`, i.e., they cannot do better than nLogn.
4
4
5
-
Counting sort is a linear time sorting algorithm that sort in O(n+k) time when elements are in the range from 1 to k.
5
+
Counting sort is a linear time sorting algorithm that sort in `O(n+k)` time when elements are in the range from 1 to k.
6
6
7
7
What if the elements are in the range from 1 to n2?
8
-
We can’t use counting sort because counting sort will take O(n2) which is worse than comparison-based sorting algorithms. Can we sort such an array in linear time?
8
+
We can’t use counting sort because counting sort will take `O(n2)` which is worse than comparison-based sorting algorithms. Can we sort such an array in linear time?
9
9
10
10
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.
11
11
@@ -16,7 +16,7 @@ Sort input array using counting sort (or any stable sort) according to the i’t
16
16
Example:
17
17
18
18
Original, unsorted list:
19
-
170, 45, 75, 90, 802, 24, 2, 66
19
+
`170, 45, 75, 90, 802, 24, 2, 66`
20
20
21
21
Sorting by least significant digit (1s place) gives:
22
22
[*Notice that we keep 802 before 2, because 802 occurred
@@ -27,98 +27,19 @@ Sorting by next digit (10s place) gives:
27
27
[*Notice that 802 again comes before 2 as 802 comes before
28
28
2 in the previous list.]
29
29
30
-
802, 2, 24, 45, 66, 170, 75, 90
30
+
`802, 2, 24, 45, 66, 170, 75, 90`
31
31
32
32
Sorting by the most significant digit (100s place) gives:
33
-
2, 24, 45, 66, 75, 90, 170, 802
33
+
`2, 24, 45, 66, 75, 90, 170, 802`
34
34
What is the running time of Radix Sort?
35
-
Let there be d digits in input integers. Radix Sort takes O(d*(n+b)) time where b is the base for representing numbers, for example, for the decimal system, b is 10.
36
-
What is the value of d? If k is the maximum possible value, then d would be O(logb(k)). So overall time complexity is O((n+b) * logb(k)). Which looks more than the
35
+
Let there be d digits in input integers. Radix Sort takes `O(d*(n+b))` time where b is the base for representing numbers, for example, for the decimal system, b is 10.
36
+
What is the value of d? If `k` is the maximum possible value, then d would be `O(logb(k))`. So overall time complexity is `O((n+b) * logb(k))`. Which looks more than the
37
37
time complexity of comparison-based sorting algorithms for a large k. Let us first limit k. Let k <= nc where c is a constant. In that case, the complexity becomes
38
-
O(nLogb(n)). But it still doesn’t beat comparison-based sorting algorithms.
38
+
`O(n logb(n))`. But it still doesn’t beat comparison-based sorting algorithms.
39
39
40
40
Is Radix Sort preferable to Comparison based sorting algorithms like Quick-Sort?
41
-
If we have log2n bits for every digit, the running time of Radix appears to be better than Quick Sort for a wide range of input numbers. The constant factors hidden in
41
+
If we have `log2n` bits for every digit, the running time of Radix appears to be better than Quick Sort for a wide range of input numbers. The constant factors hidden in
42
42
asymptotic notation are higher for Radix Sort and Quick-Sort uses hardware caches more effectively. Also, Radix sort uses counting sort as a subroutine and counting sort
43
43
takes extra space to sort numbers.
44
44
45
-
Implementation of Radix Sort
46
-
47
-
#include <iostream>
48
-
using namespace std;
49
-
50
-
// A utility function to get maximum value in arr[]
51
-
int getMax(int arr[], int n)
52
-
{
53
-
int mx = arr[0];
54
-
for (int i = 1; i < n; i++)
55
-
if (arr[i] > mx)
56
-
mx = arr[i];
57
-
return mx;
58
-
}
59
-
60
-
// A function to do counting sort of arr[] according to
61
-
// the digit represented by exp.
62
-
void countSort(int arr[], int n, int exp)
63
-
{
64
-
int output[n]; // output array
65
-
int i, count[10] = { 0 };
66
-
67
-
// Store count of occurrences in count[]
68
-
for (i = 0; i < n; i++)
69
-
count[(arr[i] / exp) % 10]++;
70
-
71
-
// Change count[i] so that count[i] now contains actual
72
-
// position of this digit in output[]
73
-
for (i = 1; i < 10; i++)
74
-
count[i] += count[i - 1];
75
-
76
-
// Build the output array
77
-
for (i = n - 1; i >= 0; i--) {
78
-
output[count[(arr[i] / exp) % 10] - 1] = arr[i];
79
-
count[(arr[i] / exp) % 10]--;
80
-
}
81
-
82
-
// Copy the output array to arr[], so that arr[] now
83
-
// contains sorted numbers according to current digit
84
-
for (i = 0; i < n; i++)
85
-
arr[i] = output[i];
86
-
}
87
-
88
-
// The main function to that sorts arr[] of size n using
89
-
// Radix Sort
90
-
void radixsort(int arr[], int n)
91
-
{
92
-
// Find the maximum number to know number of digits
93
-
int m = getMax(arr, n);
94
-
95
-
// Do counting sort for every digit. Note that instead
96
-
// of passing digit number, exp is passed. exp is 10^i
0 commit comments