8000 remove implementation · bloomsky9/Algorithms-Explanation@5937f76 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5937f76

Browse files
authored
remove implementation
1 parent c29bc0c commit 5937f76

File tree

1 file changed

+10
-89
lines changed

1 file changed

+10
-89
lines changed

en/Sorting Algorithms/Radix Sort

Lines changed: 10 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
Radix Sort
22

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

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

77
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?
99

1010
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.
1111

@@ -16,7 +16,7 @@ Sort input array using counting sort (or any stable sort) according to the i’t
1616
Example:
1717

1818
Original, unsorted list:
19-
170, 45, 75, 90, 802, 24, 2, 66
19+
`170, 45, 75, 90, 802, 24, 2, 66`
2020

2121
Sorting by least significant digit (1s place) gives:
2222
[*Notice that we keep 802 before 2, because 802 occurred
@@ -27,98 +27,19 @@ Sorting by next digit (10s place) gives:
2727
[*Notice that 802 again comes before 2 as 802 comes before
2828
2 in the previous list.]
2929

30-
802, 2, 24, 45, 66, 170, 75, 90
30+
`802, 2, 24, 45, 66, 170, 75, 90`
3131

3232
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`
3434
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
3737
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.
3939

4040
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
4242
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
4343
takes extra space to sort numbers.
4444

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
97-
// where i is current digit number
98-
for (int exp = 1; m / exp > 0; exp *= 10)
99-
countSort(arr, n, exp);
100-
}
101-
102-
// A utility function to print an array
103-
void print(int arr[], int n)
104-
{
105-
for (int i = 0; i < n; i++)
106-
cout << arr[i] << " ";
107-
}
108-
109-
// Driver Code
110-
int main()
111-
{
112-
int arr[] = { 170, 45, 75, 90, 802, 24, 2, 66 };
113-
int n = sizeof(arr) / sizeof(arr[0]);
114-
115-
// Function Call
116-
radixsort(arr, n);
117-
print(arr, n);
118-
return 0;
119-
}
120-
121-
Output
122-
2 24 45 66 75 90 170 802
123-
12445
Video reference: https://youtu.be/nu4gDuFabIM

0 commit comments

Comments
 (0)
0