Advertisement
Guest User

c++ algo

a guest
Nov 9th, 2021
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.65 KB | None | 0 0
  1. /*
  2. *
  3. * insertion Sort Algorithim
  4. *
  5. */
  6. #include <iostream>
  7. using namespace std;
  8. void insertionSort();
  9. int const n = 5;
  10. int A[n];
  11.  
  12.  
  13. void main() {
  14.  
  15.     for (int i = 0; i < n; i++) {
  16.         cin >> A[i];
  17.     }
  18.  
  19.     insertionSort();
  20.  
  21.     for (int i = 0; i < n; i++) {
  22.     cout << A[i] << "\t";
  23.     }
  24.     cout << endl;
  25.  
  26.     system("pause");
  27. }
  28.  
  29. void insertionSort() {
  30.  
  31.     for (int j = 1; j < n; j++) {
  32.  
  33.         int temp = A[j];
  34.         int i;
  35.  
  36.         for (i = j - 1; (i >= 0) && (temp > A[i]); i--) {
  37.  
  38.             A[i + 1] = A[i];//shift
  39.  
  40.             /*
  41.             for (int i = 0; i < n; i++) {
  42.                 cout << A[i] << "\t";
  43.                 cout << endl;
  44.             }
  45.             */
  46.         }
  47.         A[i + 1] = temp; // insertion
  48.     }
  49. }
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement