一,计数排序
[cpp] view plaincopy
- #include <stdio.h>
- void COUNTINGSORT(int *A, int *B, int array_size, int k)
- {
- int C[k+1], i, value, pos;
- for(i=0; i<=k; i++)
- {
- C[i] = 0;
- }
- for(i=0; i< array_size; i++)
- {
- C[A[i]] ++;
- }
- for(i=1; i<=k; i++)
- {
- C[i] = C[i] + C[i-1];
- }
- for(i=array_size-1; i>=0; i--)
- {
- value = A[i];
- pos = C[value];
- B[pos-1] = value;
- C[value]--;
- }
- }
- int main()
- {
- int A[8] = {2, 5, 3, 0, 2, 3, 0, 3}, B[8], i;
- COUNTINGSORT(A, B, 8, 5);
- for (i=0; i<= 7; i++)
- {
- printf("%d ", B[i]);
- }
- printf("\n");
- return 0;
- }
适用范围:待排序的数最大值和最小值之差大小有限。
时间: 2024-12-21 08:46:41