fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6.  
  7. void insertion_sort(int* input, int size)
  8. {
  9. for (int i=1; i<size; i++)
  10. {
  11. for (int j=i; j>0 && input[j] < input[j-1]; j--)
  12. swap(input[j], input[j-1]);
  13. }
  14. }
  15.  
  16. void print(int* input, int size)
  17. {
  18. for (int i=0; i<size; i++)
  19. cout << input[i] << " ";
  20. cout << endl;
  21. }
  22.  
  23. void populate(int* input, int size)
  24. {
  25. for (int i=0; i<size; i++)
  26. input[i] = rand() % 100;
  27. }
  28.  
  29. int main(void)
  30. {
  31. int buf[5];
  32. populate(buf, 5);
  33. print(buf, 5);
  34. insertion_sort(buf, 5);
  35. print(buf, 5);
  36.  
  37. return 0;
  38. }
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
83 86 77 15 93 
15 77 83 86 93