-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRadixSort.cpp
More file actions
39 lines (32 loc) · 876 Bytes
/
RadixSort.cpp
File metadata and controls
39 lines (32 loc) · 876 Bytes
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
#include "RadixSort.h"
using namespace std;
///Use our own knowledge and online resources
int getMax(vector<Patient*>& P) {
int max = P[0]->BMI;
for (int i = 1; i < P.size(); i++){
if (P[i]->BMI > max){
max = P[i]->BMI;
}
}
return max;
}
void countingSort(vector<Patient*>& P, int exp){
int n = P.size();
vector<Patient*> output(n);
vector<int> count(10, 0);
for (const auto& p : P)
count[(p->BMI / exp) % 10]++;
for (int i = 1; i < 10; i++)
count[i] += count[i - 1];
for (int i = n - 1; i >= 0; i--) {
int index = (P[i]->BMI / exp) % 10;
output[count[index] - 1] = P[i];
count[index]--;
}
P = output;
}
void radixSort(vector<Patient*>& P) {
int maxBMI = getMax(P);
for (int exp = 1; maxBMI / exp > 0; exp *= 10)
countingSort(P, exp);
}