fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. void print1(const vector<int> &a) {
  7. for (int i = 0; i < a.size(); i++) {
  8. cout << a[i] << " ";
  9. }
  10. cout << endl;
  11. }
  12.  
  13. void sort_ins(vector<int> &a) {
  14. for (int i = 1; i < a.size(); i++) {
  15. int c = a[i];
  16. int k = i;
  17.  
  18. while (k > 0 && a[k-1] > c) {
  19. a[k] = a[k-1];
  20. k--;
  21. }
  22. a[k] = c;
  23. }
  24. }
  25.  
  26. int main() {
  27. int m;
  28. cin >> m;
  29.  
  30. vector<int> a (m, 0);
  31. for (int i = 0; i < a.size(); i++) {
  32. cin >> a[i];
  33. }
  34.  
  35. print1(a);
  36.  
  37. sort_ins(a);
  38.  
  39. print1(a);
  40.  
  41. return 0;
  42. }
Success #stdin #stdout 0.01s 5292KB
stdin
10
18 -3 14 5 25 69 108 34 21 96
stdout
18 -3 14 5 25 69 108 34 21 96 
-3 5 14 18 21 25 34 69 96 108