fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. int n;
  6. int i,j,temp;
  7.  
  8. cout << "Please enter array elements size to sort" << endl;
  9. cin >> n;
  10.  
  11. int a[n];
  12.  
  13. cout << endl << "Please enter array of size " << n << endl;
  14.  
  15. for(i=0;i<n;i++){
  16. cin >> a[i];
  17. }
  18.  
  19. cout << endl << "the array before sorting is: " << endl;
  20.  
  21. for(i=0;i<n;i++){
  22. cout << a[i] << "\t";
  23. }
  24.  
  25. /* insertion sort */
  26. for(i=1;i<n;i++){
  27. for(j=0;j<i;j++){
  28. if(a[i] < a[j]){
  29. temp = a[i];
  30. a[i] = a[j];
  31. a[j] = temp;
  32. }
  33. }
  34. }
  35.  
  36. cout << endl << "the array after sorting is: " << endl;
  37.  
  38. for(i=0;i<n;i++){
  39. cout << a[i] << "\t";
  40. }
  41.  
  42. return 0;
  43. }
Success #stdin #stdout 0s 3472KB
stdin
5
1
4
5
6
7
8
8
7
2
1
3
4
5
6
stdout
Please enter array elements size to sort

Please enter array of size 5

the array before sorting is: 
1	4	5	6	7	
the array after sorting is: 
1	4	5	6	7