Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
radix.cpp
Go to the documentation of this file.
1
29#include <deque>
30#include <list>
31#include <vector>
32#include <algorithm>
33#include <numeric>
34#include <iostream>
35#include <iterator>
36
37#include <examples.h>
38
39typedef std::deque<int, std::allocator<int> > IntDeque;
40typedef std::vector<IntDeque, std::allocator<IntDeque> > DeqVector;
41typedef std::list<int, std::allocator<int> > IntList;
42typedef std::ostream_iterator<int, char, std::char_traits<char> > OstreamIter;
43
44
46public:
47 copyIntoBuckets (int d, DeqVector &b, bool &f)
48 : divisor (d), buckets (b), flag (f)
49 {}
50
51 void operator () (unsigned int v) {
52 const int inx = (v / divisor) % 10;
53 // flag set to true if any bucket other than the 0-th one is used
54 if (inx)
55 flag = true;
56 buckets [inx].push_back (v);
57 }
58
61 bool &flag;
62};
63
64
65IntList::iterator
66copyList (IntList::iterator c, IntDeque &lst) {
67
68 return std::copy (lst.begin (), lst.end (), c);
69}
70
71
72void radixSort (IntList & values) {
73 bool flag = true;
74 int divisor = 1;
75
76 while (flag)
77 {
78 DeqVector buckets (10);
79 flag = false;
80
81 std::for_each (values.begin (), values.end (),
82 copyIntoBuckets (divisor, buckets, flag));
83 std::accumulate (buckets.begin (), buckets.end (), values.begin (), copyList);
84 divisor *= 10;
85 std::copy (values.begin (), values.end (), OstreamIter (std::cout, " "));
86 std::cout << std::endl;
87 }
88}
89
90
91int main () {
92
93 std::cout << "Radix sort program" << std::endl;
94
95 const IntList::value_type data[] = { 624, 852, 426, 987, 269,
96 146, 415, 301, 730, 78, 593 };
97
98 IntList values (data, data + sizeof data / sizeof *data);
99
100 radixSort (values);
101 std::copy (values.begin (), values.end (), OstreamIter (std::cout, " "));
102
103 std::cout << std::endl << "End radix sort program" << std::endl;
104
105 return 0;
106}
#define d
Definition 17.names.cpp:115
#define v
Definition 17.names.cpp:138
#define f
Definition 17.names.cpp:119
#define c
Definition 17.names.cpp:111
#define b
Definition 17.names.cpp:110
#define inx
Definition 17.names.cpp:484
const char *const data[]
bool & flag
Definition radix.cpp:61
DeqVector & buckets
Definition radix.cpp:60
void operator()(unsigned int v)
Definition radix.cpp:51
copyIntoBuckets(int d, DeqVector &b, bool &f)
Definition radix.cpp:47
IntList::iterator copyList(IntList::iterator c, IntDeque &lst)
Definition radix.cpp:66
std::deque< int, std::allocator< int > > IntDeque
Definition radix.cpp:39
void radixSort(IntList &values)
Definition radix.cpp:72
std::list< int, std::allocator< int > > IntList
Definition radix.cpp:41
std::ostream_iterator< int, char, std::char_traits< char > > OstreamIter
Definition radix.cpp:42
std::vector< IntDeque, std::allocator< IntDeque > > DeqVector
Definition radix.cpp:40
int main()
Definition radix.cpp:91