MCPcopy Create free account
hub / github.com/chronoxor/CppBenchmark / ShakerSort

Class ShakerSort

examples/sort.cpp:90–130  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

88};
89
90class ShakerSort : public CppBenchmark::Benchmark, public SortFixture
91{
92public:
93 using Benchmark::Benchmark;
94
95protected:
96 void Run(CppBenchmark::Context& context) override
97 {
98 // Generate items to sort
99 std::generate(items.begin(), items.end(), rand);
100
101 // Start values of left and right bound
102 size_t left = 1;
103 size_t right = items.size();
104
105 // While left and right bound not met...
106 while (left < right)
107 {
108 // Bubble down minimal item to the left bound
109 for (size_t i = right; i > left; --i)
110 {
111 if (items[i - 1] < items[i - 1 - 1])
112 std::swap(items[i - 1], items[i - 1 - 1]);
113 }
114
115 // Increment left bound
116 ++left;
117
118 // Bubble up maximal item to the right bound
119 for (size_t i = left; i < right; ++i)
120 {
121 if (items[i + 1 - 1] < items[i - 1])
122 std::swap(items[i + 1 - 1], items[i - 1]);
123 }
124
125 // Decrement right bound
126 --right;
127 }
128 context.metrics().AddItems(items.size());
129 }
130};
131
132class GnomeSort : public CppBenchmark::Benchmark, public SortFixture
133{

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected