Algorithmic Implementation series(8) Implementation of Counting_Sort
来源:互联网 发布:snare for windows 编辑:程序博客网 时间:2024/06/06 00:04
Compiler: gcc 4.7.3
C++ Standard: C++0x
OS: CentOS 6.3 x86
1 #include <iostream>
2
3 using namespace std;
4
5 void Counting_Sort(unsigned uaa[], const size_t size_of_uaa,
6 unsigned uab[], const size_t k) {
7
8 unsigned c[k + 1];
9 for(size_t i = 0; i != k + 1; ++i) {
10 c[i] = 0;
11 }
12 //c[i] now contains the number of elements equal to i.
13 for(size_t j = 0; j != size_of_uaa; ++j) {
14 ++c[uaa[j]];
15 }
16 //c[i] now contains the number of elements less than or equal to i.
17 for(size_t i = 1; i != k + 1; ++i) {
18 c[i] = c[i] + c[i - 1];
19 }
20
21 for(size_t j = size_of_uaa; j != 0; --j) {
22 uab[c[uaa[j - 1]] - 1] = uaa[j - 1];
23 --c[uaa[j - 1]];
24 }
25 }
26 //Returning the elememt of which the value is max of the array.
27 const unsigned max_element(unsigned ua[], const size_t size) {
28 if(size == 1) {
29 return ua[0];
30 }
31 unsigned max = 0;
32
33 for(size_t i = 0; i != size; ++i) {
34 if(max < ua[i]) {
35 max = ua[i];
36 }
37 }
38 return max;
39 }
40
41 int main() {
42 unsigned uaa[] = {0, 4, 5, 33, 34, 6, 7, 8, 9, 12, 98, 99, 54, 23, 121, 1};
43 const size_t size = sizeof(uaa)/sizeof(unsigned);
44 unsigned uab[size];
45
46 Counting_Sort(uaa, size, uab, max_element(uaa, size));
47
48 for(auto beg = begin(uab); beg != end(uab); ++beg) {
49 cout << *beg << " ";
50 }
51 cout << endl;
52
53 return EXIT_SUCCESS;
54 }
- Algorithmic Implementation series(8) Implementation of Counting_Sort
- Algorithmic Implementation series(3) Implementation of Merge_Sort
- Algorithmic Implementation series(5) Implementation of Heap_Sort
- Algorithmic Implementation series(6) Implementation of Quick_Sort
- Algorithmic Implementation series(7) Implementation of Randomized_Quick_Sort
- Algorithmic Implementation series(1)——Implementation of Insertion_sort
- Algorithmic Implementation series(2)——Implementation of Selection_sort
- Algorithmic Implementation series(4) Implementation of Maximum Subarray problem(O(nlgn) and O(n))
- Implementation
- implementation
- Implementation of Polymorphism
- implementation of bitmap
- Implementation of Heap Sort
- The Implementation of ALG_DES_MAC8_ISO9797_1_M2_ALG3
- Implementation of CServer
- Implementation of CClient
- Implementation Of Choreography
- MATLAB Implementation of MonoSLAM
- POJ 1703 Find them, Catch them
- Android开发_viewpager作为header加入到listview
- 一套非常适用权限系统快速开发框架源代码
- Life/Asia table struct copybook convert to java pojo automaticlly
- 经典回顾超炫酷阅读效果3D Book使用方法
- Algorithmic Implementation series(8) Implementation of Counting_Sort
- 均值、中值滤波、孤点检测
- 在linux使用RAR
- BAT
- java.sql.Date java.util.Date String 之间的转换
- web用户控件实现注册功能+前台验证
- Java SE 自学笔记8 之 final修饰符
- BI项目中常见问题---模型设计
- 1.窗体与界面设计