forked from SifanStephanie/LinearTimeSortingAlgorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountingSort.cpp
More file actions
49 lines (41 loc) · 1.2 KB
/
CountingSort.cpp
File metadata and controls
49 lines (41 loc) · 1.2 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
/**
* Learning Algorithms - From MIT courses
*
* Counting Sort operates by counting the number of objects that have
* each distinct key value, and using arithmetic on those counts to
* determine the positions of each key value in the output sequence.(from wiki)
*
* Counting Sort runs in linear time and is not a comparison sorts method!
* the Complexity is O(n + k).
* Useful for small numbers(integers) especially when k=O(n),
* large numbers may cause lots of time because k is large.
**/
int* CountingSort(int *A){
int large=0;
int i,j;
int *B=new int[N];
//first finds the largest number in A[]
for(i=0;i<N;i++){
if(A[i]>large)
large=A[i];
}
// build the aux storage
int C[large];
//initial C[]
for(i=0;i<large;i++){
C[i]=0;
}
// find the occurs time of numbers in A[]
for(j=0;j<N;j++)
C[A[j]-1]++; // A[j]-1 because the array starts from 0 ~ N-1
// get the number of elements <= i
for(i=1;i<large;i++){
C[i]+=C[i-1];
}
//make the distribution
for(j=N-1;j>=0;j--){
B[C[A[j]-1]-1]=A[j]; // -1 denotes that the array starts from 0 ~ N-1
C[A[j]-1]--;
}
return B;
}