forked from hardikagarwal2001/Hackoctober
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountingsort.cpp
More file actions
32 lines (32 loc) · 748 Bytes
/
countingsort.cpp
File metadata and controls
32 lines (32 loc) · 748 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
# include<iostream>
using namespace std;
void counting(int arr[], int n) {
int max = arr[0];
for(int i = 1; i < n; i++) {
if(max < arr[i]) {
max = arr[i];
}
}
int count[max + 1] = {0};
int b[n] = {0};
for(int i = 0; i < n; i++) {
count[arr[i]]++;
}
for(int i = 1; i <= max; i++) {
count[i] = count[i] + count[i - 1];
}
for(int i = n - 1; i >= 0; i--) {
b[--count[arr[i]]] = arr[i];
}
for(int i = 0; i < n; i++) {
arr[i] = b[i];
}
}
int main() {
int a[11] = {4, 2, 40, 10, 10, 1, 4, 2, 1, 10, 40};
counting(a, 11);
for(int i = 0; i < 11; i++) {
cout<<a[i]<<endl;
}
return 0;
}