-
Notifications
You must be signed in to change notification settings - Fork 0
/
10.sortcounting.cpp
58 lines (50 loc) · 1.26 KB
/
10.sortcounting.cpp
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
// time complexity worst: linear
// time complexity best/average:
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int arrA[] = {2,5,3,0,2,3,0,3};
int size = sizeof(arrA)/sizeof(arrA[0]);
void display(int arr[],int in,int n){
int i;
for(i=in;i<=n;i++){
cout<<arr[i]<<" ";
}
cout<<"\n";
}
void countingSort(int arrA[],int arrB[], int k){
int arrC[k+1];
int i,j;
for(i=0;i<k+1;i++){
arrC[i]=0;
}
//display(arrC,k+1);
for(j=0;j<size;j++){
arrC[arrA[j]] += 1;
//arrC contains the number of elements equal to i
//cout<<"\narrC_+1: ";
//display(arrC,0,k);
}
for(i=1;i<k+1;i++){
arrC[i] += arrC[i-1];
//arrC contains the number of elements less than or equal to i
//cout<<"\narrC_+i-1: ";
//display(arrC,0,k);
}
for(j=size-1;j>=0;j--){
arrB[arrC[arrA[j]]]=arrA[j];
arrC[arrA[j]] -= 1;
cout<<"\narrB: ";
display(arrB,1,size);
//cout<<"\narrC_+i-1: ";
//display(arrC,0,k);
}
}
int main(){
int arrB[size];
int k = 5;
cout<<"\narrA: ";
display(arrA,0,size-1);
countingSort(arrA,arrB,k);
}