Advertisement
Maco153

Insertion Sort Algorithm

Nov 3rd, 2019
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.44 KB | None | 0 0
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     int arr[5]; int key = 0; int j = 0;
  8.     cout << "Enter the 5 numbers you want to sort!\n";
  9.     for (int i = 0; i < 5; i++) {
  10.  
  11.         cin >> arr[i];
  12.         }
  13.     for (int i = 1; i < 5; i++)
  14.     {
  15.         key= arr[i];
  16.         j = i- 1;
  17.    
  18.     while (j >= 0 && arr[j] > key) {
  19.         arr[j + 1] = arr[j];
  20.         j =j- 1;
  21. }
  22.     arr[j + 1] = key;
  23.     }
  24.     for (int i = 0; i < 5; i++) {
  25.  
  26.         cout<< arr[i];
  27.     }
  28.     return 0;
  29.  
  30.  
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement