fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <memory>
  4.  
  5. std::unique_ptr<int[]> insertionSort(const int original[], size_t size)
  6. {
  7. int* result = new int[size];
  8. std::copy(original, original+size, result);
  9. for(int* p = result; p != result + size; ++p)
  10. std::rotate(std::upper_bound(result, p, *p), p, p+1);
  11. return std::unique_ptr<int[]>(result);
  12. }
  13.  
  14. int main()
  15. {
  16. int list[]={2,3,543,65,434,24,56,477,456,34,424,4546};
  17. auto sorted = insertionSort(list, 12);
  18.  
  19. for (int i = 0; i < 12; i++)
  20. std::cout << list[i] << ' ';
  21. std::cout << '\n';
  22. for (int i = 0; i < 12; i++)
  23. std::cout << sorted[i] << ' ';
  24. std::cout << '\n';
  25. }
  26.  
Success #stdin #stdout 0s 3016KB
stdin
Standard input is empty
stdout
2 3 543 65 434 24 56 477 456 34 424 4546 
2 3 24 34 56 65 424 434 456 477 543 4546