-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountingSort.cpp
More file actions
59 lines (54 loc) · 1.05 KB
/
Copy pathcountingSort.cpp
File metadata and controls
59 lines (54 loc) · 1.05 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
#include<iostream>
using namespace std;
void countSort(int arr[],int n){
int B[n];
int max = arr[0];
for (int i=0;i<n;i++){
if (arr[i]>max){
max = arr[i];
}
}
int c[max+1]={0};
cout<<endl;
for (int i=0;i<=max;i++) {
cout<<c[i]<<" ";
}
cout<<endl;
for (int i=0;i<n;i++) {
c[arr[i]] = c[arr[i]] + 1;
}
cout<<endl;
for (int i=0;i<=max;i++) {
cout<<c[i]<<" ";
}
cout<<endl;
int j =0;
for (int i = 0;i<n;) {
while(c[j]>0){
B[i] = j;
i++;
c[j] = c[j]-1;
}
j++;
}
cout<<"After sorting\n";
for (int i=0;i<n;i++){
cout<<B[i]<<" ";
}
}
int main(){
int n;
cout<<"Enter the size of the array\n";
cin>>n;
int arr[n];
cout<<"Now enter the elements of the arrays\n";
for (int i=0;i<n;i++){
cin>>arr[i];
}
cout<<"Array before sorting\n";
for (int i=0;i<n;i++){
cout<<arr[i]<<" ";
}
countSort(arr,n);
return 0;
}