-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort.cpp
More file actions
76 lines (71 loc) · 1.92 KB
/
sort.cpp
File metadata and controls
76 lines (71 loc) · 1.92 KB
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
// C++ implementation of Radix Sort
#include <iostream>
#include <vector>
using namespace std;
// A function to do counting sort of arr[] according to the digit represented by exp.
vector<int> countSortcom(vector<int> arr, int exp) {
vector<int> output(arr.size()); // output array
int i, count[10] = { 0 };
cout << endl << "begin cycle" << endl;
// Store count of occurrences in count[]
cout << "ff" << endl;
for (i = 0; i < arr.size(); i++) {
cout << "filling count" << endl;
count[(arr[i] / exp) % 10]++; // (arr[i] / exp) % 10 - current digit
cout << "index: " << (arr[i] / exp) % 10 << ", val: " << count[(arr[i] / exp) % 10] << "; ";
}
cout << "ff end" << endl;
// Change count[i] so that count[i] now contains actual
// position of this digit in output[]
cout << "sdvig start" << endl;
for (i = 1; i < 10; i++) {
cout << count[i] << " + " << count[i - 1] << " = " << count[i] + count[i - 1] << endl;
count[i] += count[i - 1];
}
cout << "sdv end" << endl;
// Build the output array
for (i = arr.size() - 1; i >= 0; i--) {
cout << "output[" << count[(arr[i] / exp) % 10] - 1 << "] = " << arr[i] << endl;
output[count[(arr[i] / exp) % 10] - 1] = arr[i];
count[(arr[i] / exp) % 10]--;
}
return output;
}
void radix_sort(int *a, int n) {
int i = 0, b[100], m = 0, exp = 1;
for (i = 0; i < n; i++)
if (a[i] > m)
m = a[i];
for (exp; m / exp > 0; exp *= 10) {
int box[10] = { 0 };
i = 0;
for (i = 0; i < n; i++) {
box[(a[i] / exp) % 10]++;
}
for (i = 1; i < 10; i++) {
box[i] += box[i - 1];
}
for (i = n - 1; i >= 0; i--) {
b[--box[(a[i] / exp) % 10]] = a[i];
}
for (i = 0; i < n; i++) {
a[i] = b[i];
}
}
}
// Driver Code
int mainn()
{
cout << "radix sort" << endl << "write numbers" << endl;
int arr[100] = {0};
int n=0,x;
while (cin>>x) {
arr[n]=x;
n++;
}
radix_sort(arr,n);
cout << "output: " << endl;
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}