fork download
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. void insertion_sort(int n, int arr[])
  6. {
  7. cout<<"insertion sort"<<endl;
  8.  
  9. for(int j=1;j<n;j++)
  10. {
  11. int key=arr[j];
  12. int i=j-1;
  13. while(i>=0 && arr[i]>key)
  14. {
  15. arr[i+1]=arr[i];
  16. i--;
  17. }
  18. arr[i+1]=key;
  19. }
  20. }
  21.  
  22. int main()
  23. {
  24. int n=5;
  25. int arr[5]={5,3,6,7,1};
  26.  
  27. insertion_sort(n,arr);
  28.  
  29. for(int i=0;i<n;i++)
  30. {
  31. cout<<arr[i]<<" ";
  32.  
  33. }
  34. }
  35.  
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
insertion sort
1 3 5 6 7