-
Notifications
You must be signed in to change notification settings - Fork 4
/
9、countSort.c
51 lines (40 loc) · 851 Bytes
/
9、countSort.c
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
/*
计数排序,时间复杂度:O(n)
*/
#include<stdio.h>
#include<malloc.h>
void countSort(int *a, int count);
void showArray(int *a, int count);
void showArray(int *a, int count){
int i;
for(i = 0; i < count; i++){
printf("%d ", a[i]);
}
printf("\n");
}
void countSort(int *a, int count){
int b[10] = {0};
int *c;
int i;
c = (int *)malloc(sizeof(int) * count);
for(i = 0; i < count; i++){
b[a[i]]++;
}
for(i = 1; i < 10; i++){
b[i] += b[i-1];
}
for(i = count-1; i >= 0; i--){
c[b[a[i]]-1] = a[i];
b[a[i]]--;
}
for(i = 0; i < count; i++){
a[i] = c[i];
}
free(c);
}
void main(void){
int a[] = {2, 4, 7, 2, 1, 0, 9};
int count = sizeof(a)/sizeof(int);
countSort(a, count);
showArray(a, count);
}