| 306 | }; |
| 307 | |
| 308 | class QuickSort : public CppBenchmark::Benchmark, public SortFixture |
| 309 | { |
| 310 | public: |
| 311 | using Benchmark::Benchmark; |
| 312 | |
| 313 | protected: |
| 314 | void Run(CppBenchmark::Context& context) override |
| 315 | { |
| 316 | // Generate items to sort |
| 317 | std::generate(items.begin(), items.end(), rand); |
| 318 | |
| 319 | // Sort items |
| 320 | if (items.size() > 0) |
| 321 | QuickSortInternal(items, 1, items.size()); |
| 322 | context.metrics().AddItems(items.size()); |
| 323 | } |
| 324 | |
| 325 | private: |
| 326 | static void QuickSortInternal(std::vector<int>& subitems, size_t left, size_t right) |
| 327 | { |
| 328 | // Choose the pivot item |
| 329 | int pivot = subitems[left + ((right - left) / 2) - 1]; |
| 330 | |
| 331 | size_t l = left; |
| 332 | size_t r = right; |
| 333 | |
| 334 | while (l <= r) |
| 335 | { |
| 336 | // Move left if item is less than pivot |
| 337 | while ((l < right) && (subitems[l - 1] < pivot)) |
| 338 | l++; |
| 339 | // Move right if item is less than pivot |
| 340 | while ((r > left) && (subitems[r - 1] > pivot)) |
| 341 | r--; |
| 342 | |
| 343 | if (l <= r) |
| 344 | { |
| 345 | // Swap left and right items |
| 346 | std::swap(subitems[l - 1], subitems[r - 1]); |
| 347 | |
| 348 | // Move left and right indexes |
| 349 | ++l; |
| 350 | --r; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | // Perform quick sort operation for the left sub items |
| 355 | if (left < r) |
| 356 | QuickSortInternal(subitems, left, r); |
| 357 | // Perform quick sort operation for the right sub items |
| 358 | if (l < right) |
| 359 | QuickSortInternal(subitems, l, right); |
| 360 | } |
| 361 | }; |
| 362 | |
| 363 | class QuickSort3 : public CppBenchmark::Benchmark, public SortFixture |
| 364 | { |
nothing calls this directly
no outgoing calls
no test coverage detected