码迷,mamicode.com
首页 > 编程语言 > 详细

计数排序-algorithms_3th

时间:2015-02-28 20:10:56      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:

 1 #include <iostream>
 2 #include <vector>
 3 using namespace std;
 4 
 5 void COUNTING_SORT(int *const A, const int &length,int &k){
 6 
 7     vector<int> B(length, 0);
 8     vector<int> C(k, 0);
 9 
10     for (auto j = 0; j < length; ++j){
11         C[A[j]] = C[A[j]] + 1;
12     }
13     for (auto i = 1; i < k; ++i){
14         C[i] = C[i] + C[i - 1];
15     }
16 
17     for (auto j = length-1; j >= 0; --j){
18         B[C[A[j]]-1] = A[j];
19         C[A[j]] = C[A[j]] - 1;
20     }
21 
22     for (auto it = B.begin(); it != B.end(); ++it){
23         cout << *it << " ";
24     }
25     cout << endl;
26 }
27 int main(void)
28 {
29     int a[] = { 2,5,3,0,2,3,0,3 };
30     int i = 6;
31     COUNTING_SORT(a, (end(a) - begin(a)), i);
32     system("pause");
33     return 0;
34 }

 

计数排序-algorithms_3th

标签:

原文地址:http://www.cnblogs.com/lhyz/p/4306126.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!