-
Notifications
You must be signed in to change notification settings - Fork 368
/
radix_sort.cpp
80 lines (76 loc) · 2.06 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
80
// Radix sort is a sorting algorithm that sorts the elements by first grouping the individual digits of the same place value. Then, sort the elements according to their increasing/decreasing order.
// Time Complexity
// Best :O(n+k)
// Worst :O(n + k)
// Average: O(n + k)
// Space Complexity : O(max)
#include <iostream>
using namespace std;
// function to find largest value of the array
int largest(int arr[], int size)
{
int largest = arr[0];
for (int i = 1; i < size; i++)
{
if (arr[i] > largest)
{
largest = arr[i];
}
}
return largest;
}
// A function to do counting sort of arr[] according to the digit represented by exp.
void countSort(int arr[], int size, int place)
{
const int max = 10;
int output[100];
int count[max];
for (int i = 0; i < max; i++)
{
count[i] = 0;
}
// Store count of occurrences in count[]
for (int i = 0; i < size; i++)
{
count[(arr[i] / place) % 10]++;
}
// Change count[i] so that count[i] now contains actual
// position of this digit in output[]
for (int i = 1; i < max; i++)
{
count[i] += count[i - 1];
}
// Build the output array
for (int i = size - 1; i >= 0; i--)
{
output[count[(arr[i] / place) % 10] - 1] = arr[i];
count[(arr[i] / place) % 10]--;
}
// Copy the output array to arr[], so that arr[] now
// contains sorted numbers according to current digit
for (int i = 0; i < size; i++)
{
arr[i] = output[i];
}
}
// Radix Sort Function
void radixsort(int arr[], int size)
{
// Get maximum element
int max = largest(arr, size);
// Apply counting sort to sort elements based on place value.
for (int place = 1; max / place > 0; place *= 10)
countSort(arr, size, place);
}
// main function
int main()
{
int arr[] = {121, 432, 564, 23, 1, 45, 788};
int size = sizeof(arr) / sizeof(arr[0]);
radixsort(arr, size);
int i;
for (i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
return 0;
}