task6.cpp 813 B

12345678910111213141516171819202122232425262728293031323334353637
  1. #include <iostream>
  2. void quickSort(int arr[], int low, int high) {
  3. if (low < high) {
  4. int pivot = arr[high];
  5. int i = low - 1;
  6. for (int j = low; j < high; j++) {
  7. if (arr[j] < pivot) {
  8. i++;
  9. std::swap(arr[i], arr[j]);
  10. }
  11. }
  12. std::swap(arr[i + 1], arr[high]);
  13. int partitionIndex = i + 1;
  14. quickSort(arr, low, partitionIndex - 1);
  15. quickSort(arr, partitionIndex + 1, high);
  16. }
  17. }
  18. int main() {
  19. int arr[] = {12, 11, 13, 5, 6, 7};
  20. int n = sizeof(arr) / sizeof(arr[0]);
  21. quickSort(arr, 0, n - 1);
  22. std::cout << "Отсортированный массив: ";
  23. for (int i = 0; i < n; i++) {
  24. std::cout << arr[i] << " ";
  25. }
  26. std::cout << std::endl;
  27. return 0;
  28. }