C++排序算法之计数排序

来源:互联网 发布:淘宝外卖网址 编辑:程序博客网 时间:2024/06/05 04:36
计数排序

像快排、堆排、归并等排序算法都是基于比较的排序算法,时间复杂度最好情况也只能降到O(nlogn)。
计数排序是一种线性排序算法,不需要进行比较,时间复杂度为O(n)。(注意是计数排序不是基数排序,两者不同
基本思想是:对于每个元素x,找出比x小的数的个数,从而确定x在排好序的数组中的位置。此算法需要辅助数组,是以空间换时间。
举例说明:
原始数组:arr
1
5
3
7
6
2
8
9
4
3
3

其中最大的元素为9,那么就申请一个大小为9的辅助数组count作为计数数组,同时拷贝一份原始数组元素tmp,
辅助数组tmp
1
5
3
7
6
2
8
9
4
3
3
然后遍历整个原始数组元素,将元素作为count的下标,统计每个元素出现的次数。
辅助数组count
0
1
1
3
1
1
1
1
1
1
接着遍历count数组,对于每个元素x,计算出比x小的元素的个数,即count[i]=count[i]+count[i-1]。
辅助数组count
0
1
2
5
6
7
8
9
10
11
然后遍历整个原始数组元素,将对应的数字放入最终的位置上。
代码如下:
#include <iostream>#include <vector>using namespace std;void CountSort(vector<int> &arr, int maxVal) {int len = arr.size();if (len < 1)return;vector<int> count(maxVal+1, 0);vector<int> tmp(arr);for (auto x : arr)count[x]++;for (int i = 1; i <= maxVal; ++i)count[i] += count[i - 1];for (int i = len - 1; i >= 0; --i) {arr[count[tmp[i]] - 1] = tmp[i];count[tmp[i]]--;//注意这里要减1}}int main(){vector<int> arr = { 1,5,3,7,6,2,8,9,4,3,3 };int maxVal = 9;CountSort(arr,maxVal);for (auto x : arr)cout << x << " ";cout << endl;return 0;}
运行结果如下:
此算法适合最大值不大的情况,如果元素个数n不大,但是最大值很大,会造成空间浪费

原创粉丝点击