-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRadix_sort.cpp
79 lines (66 loc) · 1.66 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
using namespace std;
void countSort(int arr[], int n, int exp)
{
int count[10] = {0};
// maximum array of one's 10th or 100th place will be alwasy 10
// Store the count of each digit in the count array
for (int i = 0; i < n; i++)
{
count[(arr[i] / exp) % 10]++;
}
// Modify the count array to store the actual position of each digit in the sorted array
for (int i = 1; i < 10; i++)
{
count[i] += count[i - 1];
}
// Create a temporary array to store the sorted array
int temp[n];
// Build the sorted array
for (int i = n - 1; i >= 0; i--)
{
temp[count[(arr[i] / exp) % 10] - 1] = arr[i]; // the sorting logic is to
count[(arr[i] / exp) % 10]--;
}
// Copy the sorted array back to the original array
for (int i = 0; i < n; i++)
{
arr[i] = temp[i];
}
}
void radixsort(int arr[], int n)
{
float max = arr[0]; // Find the maximum element in the array
for (int i = 1; i < n; i++)
{
if (arr[i] > max)
{
max = arr[i]; // Find the maximum element in the array
}
}
// Perform counting sort for each digit place
for (int exp = 1; max / exp > 0; exp *= 10)
{
countSort(arr, n, exp);
cout << "hi";
}
}
int main()
{
int n;
cout << "Enter the number of elements in the array: ";
int arr[n];
cin >> n;
for (int i = 0; i < n; i++)
{
cout << "Enter the element " << i + 1 << ": ";
cin >> arr[i];
}
radixsort(arr, n);
for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
cout << endl;
return 0;
}