标签:
各种排序算法都有各自的使用范围。例如快速排序,如果数组本身已经排好序了,那么再使用它进行排序工作量为 O(n^2)。
//对公司所有员工的年龄进行排序 //假定,所有员工的年龄从15岁到60岁不等 //非常简单,没什么可说的 void SortAge(int data[], int length) { const int Youngest = 15, Oldest = 60; if (data == NULL || length <= 0) return; int Age[Oldest - Youngest + 1]; for (int i = Youngest; i <= Oldest; ++i) { Age[i] = 0; } for (int i = 0; i < length; ++i) { int age = data[i]; if (age<Youngest || age>Oldest) throw exception("age out of range"); Age[age]++; } int index = 0; for (int i = Youngest; i < Oldest; ++i) { for (int j = 0; j < Age[i]; j++) { data[index++] = i; } } }
标签:
原文地址:http://www.cnblogs.com/chengxuyuanxiaowang/p/4321895.html