GarikK

Сортировка массивов(вставка и пузырьковая)

Mar 11th, 2020
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | None | 0 0
  1. // PovTor.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3.  
  4. #include <iostream>
  5. using namespace std;
  6. int main()
  7. {
  8.     const int Size = 10;
  9.     int arr[Size] = {9,8,7,6,5,4,3,2,1,0};
  10.     //vstavka
  11.     //---
  12.    
  13.     for (auto i = 1; i < Size; i++)
  14.     {
  15.         int tmp = arr[i];
  16.         int j = i - 1;
  17.         while (j >= 0 && arr[j] > tmp)
  18.         {
  19.             arr[j + 1] = arr[j];
  20.             arr[j] = tmp;
  21.             j--;
  22.         }
  23.     }
  24.     for (auto i = 0; i <Size; i++)
  25.     {
  26.         cout << arr[i] << endl;
  27.     }
  28.     //Bubble
  29.     //---
  30.     for (auto i = 0; i < Size - 1; i++)
  31.     {
  32.         for (auto j = 0; j < (Size - 1) - i; j++)
  33.         {
  34.             if (arr[j] > arr[j + 1])
  35.             {
  36.                 int tmp = arr[j];
  37.                 arr[j] = arr[j + 1];
  38.                 arr[j + 1] = tmp;
  39.             }
  40.         }
  41.  
  42.     }
  43.     for (auto i = 0; i < Size; i++)
  44.     {
  45.         cout << arr[i] << endl;
  46.     }
  47. }
Add Comment
Please, Sign In to add comment