fork download
  1. #include<iostream>
  2. using namespace std;
  3. main()
  4. {
  5. int Key;
  6. int array[8];
  7. cout<<"Enter 8 numbers: "<<endl;
  8. for(int i=0; i<8; i++)
  9. {
  10. cin>>array[i];
  11. }
  12. cout<<endl;
  13. cout<<"Orignally entered array by the user is: "<<endl;
  14. for(int j=0; j<8; j++)
  15. {
  16. cout<<array[j];
  17. cout<<endl;
  18. }
  19. cout<<endl;
  20. for(int j=1 ; j < 8 ; j++)
  21. { Key = array[j];
  22. int i = j-1;
  23. while(i >= 0 && array[i] > Key)
  24. {
  25. array[i + 1] = array[i];
  26. i = i - 1;
  27. }
  28. array[i + 1] = Key;
  29. }
  30. cout<<"Sorted Array is: "<<endl;
  31. for(int i=0; i<8; i++)
  32. {
  33. cout<<array[i]<<endl;
  34. }
  35. }
Success #stdin #stdout 0s 5428KB
stdin
1
5
6
9
7
8
3
2
stdout
Enter 8 numbers: 

Orignally entered array by the user is: 
1
5
6
9
7
8
3
2

Sorted Array is: 
1
2
3
5
6
7
8
9