fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. int n;
  6. cin>>n;
  7. int * arr = new int [n];
  8. for(int i = 0; i<n; i++){
  9. cin>>arr[i];
  10. }
  11. for(int i = 1; i<n; i++){
  12. int key = arr[i];
  13. int j = i-1;
  14. while(j>=0 && arr[j] > key){
  15. arr[j+1] = arr[j];
  16. j = j-1;
  17. }
  18. arr[j+1] = key;
  19. }
  20. for(int i = 0 ; i<n; i++ ){
  21. cout << arr[i] << " ";
  22. }
  23. delete [] arr;
  24.  
  25. return 0;
  26. }
Success #stdin #stdout 0.01s 5288KB
stdin
5
5 1 6 2 3
stdout
1 2 3 5 6