-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcounting_sort.cpp
More file actions
44 lines (38 loc) · 902 Bytes
/
counting_sort.cpp
File metadata and controls
44 lines (38 loc) · 902 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
40
41
42
43
44
#include <iostream>
using namespace std;
void countsort(int arr[],int n){
//finding maximum
int max=0;
for(int i=1;i<n;i++){
if(max<arr[i]){
max=arr[i];
}
}
//making count array and store count of element in arr
int *count=(int*)calloc(max+1,sizeof(int));
for(int i=0;i<n;i++){
count[arr[i]]++;
}
//finding cummalative sum
for(int i=1;i<=max;i++){
count[i]+=count[i-1];
}
//output array and their updation
int* output = (int*)malloc(n * sizeof(int));
for(int i=n-1;i>=0;i--){
output[--count[arr[i]]]=arr[i];
}
string sorted sequence in main array
for(int i=0;i<n;i++){
arr[i]=output[i];
}
}
int main() {
int arr[]={1,3,2,6,4};
int n=sizeof(arr)/sizeof(arr[0]);
countsort(arr,n);
for(int i=0;i<n;i++){
cout<<arr[i]<<"\t";
}
return 0;
}