AmateurKid

Untitled

Nov 7th, 2021 (edited)
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.     int arr_size;
  9.     cout << "Input the number integers> ";
  10.     cin >> arr_size;
  11.     int* arr = new int[arr_size];
  12.     int i,j;
  13.     string quest;
  14.     //char answ2;
  15.     for (i = 0; i < arr_size; i++) {
  16.         cout << "Input the integer # " << i + 1 << " ";
  17.         cin >> arr[i];
  18.     }
  19.     cout << "Current array: "; //выводим введённый массив
  20.     for (i = 0; i < arr_size; i++) {
  21.  
  22.         cout << arr[i] << " ";
  23.     }
  24.  
  25.     int first;
  26.     int second;
  27.  
  28.         cout << "\nInput bounds of the segment> "; //обозначаем границы сортировки (первая и вторая координаты)
  29.         cin >> first >> second;
  30.         cout << "\nInput an order of sorting (asc/desc)> "; //тип сортировки (asc - по возрастанию, desc - по убыванию)
  31.         cin >> quest;
  32.  
  33.         first -= 1; //т.к координаты массива начинаются с нуля, отнимаем единицу
  34.         second -= 1;
  35.  
  36.         j = first;
  37.         i = first;
  38.  
  39.         if (quest == "desc") { //сортировка пузырьком
  40.             for (j = first; j < second; j++) {
  41.                 for (i = first; i < second - (i + 1); i++) {
  42.                     if (arr[i] > arr[i + 1]) {
  43.                         swap(arr[i], arr[i + 1]);
  44.                     }
  45.                 }
  46.             }
  47.         }
  48.  
  49.         if (quest == "asc") { //тоже сортировка пузырьком
  50.             for (j = first; j < second; j++) {
  51.                 for (i = first; i < second - (i + 1); i++) {
  52.                     if (arr[i] < arr[i + 1]) {
  53.                         swap(arr[i], arr[i + 1]);
  54.                     }
  55.                 }
  56.             }
  57.         }
  58.  
  59.  
  60.  
  61.  
  62.         for (i = 0; i < arr_size; i++) {
  63.             cout << arr[i] << " ";
  64.         }
  65.  
  66.         /*cout << "\nContinue? (Y/N)> ";
  67.         cin >> answ2;*/
  68.  
  69.  
  70.     return 0;
  71. }
  72.  
  73.  
  74.  
  75.  
Add Comment
Please, Sign In to add comment