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

Class BubbleSort

examples/sort.cpp:61–88  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

59};
60
61class BubbleSort : public CppBenchmark::Benchmark, public SortFixture
62{
63public:
64 using Benchmark::Benchmark;
65
66protected:
67 void Run(CppBenchmark::Context& context) override
68 {
69 // Generate items to sort
70 std::generate(items.begin(), items.end(), rand);
71
72 // Iterate through all items to get the current bound
73 for (size_t i = 0; i < items.size(); ++i)
74 {
75 // Calculate the current bound
76 size_t bound = items.size() - i;
77
78 // Starting from the current items and for all next items before the bound...
79 for (size_t j = 1; j < bound; ++j)
80 {
81 // Swap items to bubble up minimal one to the end
82 if (items[j] < items[j - 1])
83 std::swap(items[j], items[j - 1]);
84 }
85 }
86 context.metrics().AddItems(items.size());
87 }
88};
89
90class ShakerSort : public CppBenchmark::Benchmark, public SortFixture
91{

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected