ADD file via upload

master
pv6bcg5ps 4 years ago
parent 61c39e5977
commit d7a5f6cbc1

@ -0,0 +1,56 @@
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
void CountSort(int *a, int len)
{
assert(a);
int max = a[0], min = a[0];
for (int i = 0; i < len; i++){
if (a[i] > max)
max = a[i];
if (a[i] < min)
min = a[i];
}
int range = max - min + 1;
int *b = (int *)calloc(range, sizeof(int));
for (int i = 0; i < len; i++){
b[a[i] - min] += 1;
}
int j = 0;
for (int i = 0; i < range; i++){
while (b[i]--){
a[j++] = i + min;
}
}
free(b);
b = NULL;
}
void PrintArray(int *a, int len)
{
for (int i = 0; i < len; i++){
printf("%d ", a[i]);
}
printf("\n");
}
int main()
{
int a[] = { 3, 4, 3, 2, 1, 2, 6, 5, 4, 7 };
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><EFBFBD>");
PrintArray(a, sizeof(a) / sizeof(int));
CountSort(a, sizeof(a) / sizeof(int));
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
PrintArray(a, sizeof(a) / sizeof(int));
system("pause");
return 0;
}
Loading…
Cancel
Save