Advertisement
mhdew

Insertion Sort

Dec 9th, 2018
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.49 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. void insertionSort(int a[], int n)
  6. {
  7.     int j;
  8.     for(int i=1;i<n;i++){
  9.         int temp=a[i];
  10.         for(j=i-1;j>=0 && temp<a[j];j--){
  11.             a[j+1]=a[j];
  12.         }
  13.         a[j+1]=temp;
  14.     }
  15. }
  16.  
  17. int main()
  18. {
  19.     int n;
  20.     scanf("%d", &n);
  21.     int a[n+3];
  22.     for(int i=0;i<n;i++) scanf("%d", &a[i]);
  23.  
  24.     insertionSort(a,n);
  25.  
  26.     for(int i=0;i<n;i++) printf("%d ", &a[i]);
  27.     printf("\n");
  28.  
  29.  
  30.     return 0;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement