fork(1) download
  1. // Example program
  2. #include <iostream>
  3. using namespace std;
  4. void swap(int *x, int *y)
  5. {
  6. int temp = *x;
  7. *x = *y;
  8. *y = temp;
  9. }
  10. int main()
  11. {
  12. int a[]{2,1,9,5,6,2,5,8,4,2};
  13. int n = sizeof(a)/sizeof(a[0]);
  14. for (int i = 1; i < n; i++){
  15. int j = i;
  16. while((j > 0) && (a[j]<a[j-1])){
  17. swap(a[j],a[j-1]);
  18. j = j - 1;
  19.  
  20. }
  21. }
  22.  
  23. for (int i = 0; i < n; i++)
  24. cout<<a[i]<<" ";
  25. }
  26.  
Success #stdin #stdout 0s 4236KB
stdin
Standard input is empty
stdout
1 2 2 2 4 5 5 6 8 9