个人感觉,这个更像是索引排序: 这是一篇分析的 文章 : 我的实现: /* *作者: JoKer *时间:2011.3.25 *作用:线性排序,时间复杂度O(n) * 这是一种非比较排序,所以速度比较快,但是有限制条件的 * 这个算法要求数组中的元素全是在已知范围的集合中,这里假设从0 到 dif_num的整数。 * */ #include <iostream> using namespace std; int counting_sort( int* array, int dif_num, int len) { int* index = new int[dif_num + 1]; int* output = new int[len]; int i = 0; if(index == NULL) { cout << "alloc failed !" << endl; getchar(); exit(0); } if(output == NULL) { cout << "alloc failed!!!" << endl; getchar(); exit(0); } memset (index, 0, sizeof(int) * (dif_num + 1)); memset (output, 0, sizeof(int) * len); for (i = 0; i < len; i++) { index[array[i]]++; } for (i = 1; i < dif_num + 1; i++) { index[i] = index[i] + index[i-1]; } for (i = 0; i < len; i++) { int n_index = --index[array[i]]; output[n_index] = array[i]; } for (i = 0; i < len; i++) { cout << output[i] << " " ; } return 0; } int main() { int array[10] = {3,5,3,5,1,2,6,7,8,3}; counting_sort(array, 8, 10); getchar(); return 0; }