task7.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include <iostream>
  2. #include <algorithm>
  3. void quickSort(int arr[], int low, int high) {
  4. if (low < high) {
  5. int pivot = arr[high];
  6. int i = low - 1;
  7. for (int j = low; j < high; j++) {
  8. if (arr[j] < pivot) {
  9. i++;
  10. std::swap(arr[i], arr[j]);
  11. }
  12. }
  13. std::swap(arr[i + 1], arr[high]);
  14. int partitionIndex = i + 1;
  15. quickSort(arr, low, partitionIndex - 1);
  16. quickSort(arr, partitionIndex + 1, high);
  17. }
  18. }
  19. int main() {
  20. int arr[] = {12, -5, 3, 7, 8, -2, 4, 1, -1, 6};
  21. int n = sizeof(arr) / sizeof(arr[0]);
  22. double sum = 0;
  23. for (int i = 0; i < n; i++) {
  24. sum += arr[i];
  25. }
  26. double average = sum / n;
  27. if (average > 0) {
  28. quickSort(arr, 0, (2 * n / 3) - 1);
  29. } else {
  30. quickSort(arr, 0, (n / 3) - 1);
  31. }
  32. for (int i = (2 * n / 3); i < n; i++) {
  33. std::swap(arr[i], arr[n - 1 - (i - (2 * n / 3))]);
  34. }
  35. std::cout << "Результат: ";
  36. for (int i = 0; i < n; i++) {
  37. std::cout << arr[i] << " ";
  38. }
  39. std::cout << std::endl;
  40. return 0;
  41. }