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 }