| 379 | |
| 380 | private: |
| 381 | static void QuickSort3Internal(std::vector<int>& subitems, size_t left, size_t right) |
| 382 | { |
| 383 | // Choose the pivot item |
| 384 | int pivot = subitems[left + ((right - left) / 2) - 1]; |
| 385 | |
| 386 | size_t bl = left; |
| 387 | size_t br = right; |
| 388 | |
| 389 | size_t l = left; |
| 390 | size_t r = right; |
| 391 | |
| 392 | while (l <= r) |
| 393 | { |
| 394 | // Move left if item is less than pivot |
| 395 | while ((l < right) && (subitems[l - 1] < pivot)) |
| 396 | l++; |
| 397 | // Move right if item is less than pivot |
| 398 | while ((r > left) && (subitems[r - 1] > pivot)) |
| 399 | r--; |
| 400 | |
| 401 | if (l <= r) |
| 402 | { |
| 403 | // Swap left and right items |
| 404 | std::swap(subitems[l - 1], subitems[r - 1]); |
| 405 | |
| 406 | // Move equals to the left |
| 407 | if (subitems[l - 1] == pivot) |
| 408 | { |
| 409 | std::swap(subitems[bl - 1], subitems[l - 1]); |
| 410 | bl++; |
| 411 | } |
| 412 | |
| 413 | // Move equals to the right |
| 414 | if (subitems[r - 1] == pivot) |
| 415 | { |
| 416 | std::swap(subitems[br - 1], subitems[r - 1]); |
| 417 | br++; |
| 418 | } |
| 419 | |
| 420 | // Move left and right indexes |
| 421 | ++l; |
| 422 | --r; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | // Swap left equals with the lower items |
| 427 | for (size_t k = left; (k < bl) && (r > left); ++k, --r) |
| 428 | std::swap(subitems[k - 1], subitems[r - 1]); |
| 429 | // Swap right equals with the upper items |
| 430 | for (size_t k = right; (k > br) && (l < right); --k, ++l) |
| 431 | std::swap(subitems[k - 1], subitems[l - 1]); |
| 432 | |
| 433 | // Perform quick sort operation for the left sub items |
| 434 | if (left < r) |
| 435 | QuickSort3Internal(subitems, left, r); |
| 436 | // Perform quick sort operation for the right sub items |
| 437 | if (l < right) |
| 438 | QuickSort3Internal(subitems, l, right); |