第一次发表代码,写的还比较初级,求指教:
bucket sort 说白了就是一个对某一固定范围的数字排序,复杂度O(M+N)。
后面我再写个radix sort。
#include <stdio.h> #define MAX_VALUE 50500 /*the routine does not work for negative numbers since the array subscript does not work */ int main(void) { int arr[MAX_VALUE]; int m; //for input handling for (m=0;m<MAX_VALUE;m++) arr[m]=-1; m=0; while ( scanf("%d", &arr[m]) >0 ) m++; int arr_temp [MAX_VALUE ]; int k; //initialize the temp array to zero for (k=0;k<MAX_VALUE;k++) arr_temp[k] = 0; int i = sizeof(arr)/sizeof(int); // printf("%d ", i); int j=0; while (j <i) { if (arr[j] >=0) { arr_temp[ arr[j] ] ++; j++; } else continue; } k=0; int h=0; for (k=0;k<MAX_VALUE ;k++) { if ( arr_temp[k] != 0 ) { while ( h< arr_temp[k] ) {printf("%d ", k); //always use block structure even for only one statement, keep that in mind h++; } h=0; } else continue; } printf("\n"); return 0; }