forked from rituburman/hacktoberfest2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathradix_sort.cpp
53 lines (48 loc) · 1.1 KB
/
radix_sort.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
using namespace std;
void countSort(int arr[], int n, int place){
int range = 10;
int i;
int freq[range] = {0};
int output[n];
for(i=0; i<n; i++)
freq[(arr[i]/place)%range]++;
for(i=1; i<range; i++)
freq[i] += freq[i-1];
for(i=n-1; i>=0; i--){
output[freq[(arr[i]/place)%range]-1]=arr[i];
freq[(arr[i]/place)%range]--;
}
for(i=0; i<n; i++)
arr[i] = output[i];
}
void radixSort(int arr[], int n, int maxx){
int mul=1;
while(maxx){
countSort(arr, n ,mul);
mul *= 10;
maxx /= 10;
}
}
int getMax(int arr[], int n){
int max = arr[0];
for (int i = 1; i < n; i++)
if (arr[i] > max)
max = arr[i];
return max;
}
int main(){
int n;
int *arr;
cout << "Enter number of elements: ";
cin >> n;
arr = new int[n];
cout << "Enter elements: ";
for(int i=0; i<n; i++)
cin >> arr[i];
radixSort(arr, n, getMax(arr, n));
cout << "Radix Sorted array is: ";
for(int i=0; i<n; i++)
cout << arr[i] << " ";
return 0;
}