The value ranges from 0 to 100, and is much smaller than the number of records (tens of thousands ).
If the auxiliary space of O (n) can be used, the time complexity can be reduced to O (n ).
Use age [10] to simulate age 1-10. Number of employees in the simulation of 25 data items. Age Array records the age population indicated by subscript. After the age statistics are complete, you can sort the order and write the data array.
# Include <stdio. h> # include <stdlib. h> void sort (int data [], int length) {int age [10], I, j, c = 0; if (data = NULL | length <= 0) return; for (I = 0; I <10; I ++) {age [I] = 0 ;} for (I = 0; I <length; I ++) {if (data [I] <0 | data [I]> 10) return; age [data [I] ++ ;}for (I = 0; I <10; I ++) for (j = 0; j <age [I]; j ++) data [c ++] = I;} void main () {int d [25] =, 5, 5, 3, 9, 7, 7, 4, 6, 7, 5, 8}, I; sort (d, 25); for (I = 0; I <25; I ++) printf ("% d", d [I]);}