Advertisement
Hydrase

Insertion Sort

Aug 6th, 2024
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.46 KB | None | 0 0
  1. #include<iostream.h>
  2. #include<conio.h>
  3. int main()
  4. {
  5. int n,i,j,k,ptr,temp,a[10];
  6. clrscr();
  7. cout<<"\nEnter the value for n"<<endl;
  8. cin>>n;
  9. cout<<"Enter elements:"<<endl;
  10. for(i=0;i<n;i++)
  11. cin>>a[i];
  12. for(k=0;k<n;k++)
  13. {
  14. ptr=k-1;
  15. temp=a[k];
  16. while(temp<a[ptr]&&ptr>=0)
  17. {
  18. a[ptr+1]=a[ptr];
  19. ptr=ptr-1;
  20. }
  21. a[ptr+1]=temp;
  22. }
  23. cout<<"\nSorted array:"<<endl;
  24. for(i=0;i<n;i++)
  25. cout<<a[i]<<"\t";
  26. getch();
  27. return 0;
  28. }
  29.  
  30.  
  31.  
  32.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement