fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. void printArray(int array[], int size) {
  6. for (int i = 0; i < size; i++) {
  7. cout << array[i] << " ";
  8. }
  9. cout << endl;
  10. }
  11.  
  12. void insertionSort(int array[], int size) {
  13. for (int step = 1; step < size; step++) {
  14. int key = array[step];
  15. int j = step - 1;
  16. while (key < array[j] && j >= 0) {
  17. array[j + 1] = array[j];
  18. --j;
  19. }
  20. array[j + 1] = key;
  21. }
  22. }
  23.  
  24.  
  25. int main() {
  26. int data[] = {9, 5, 1, 4, 3};
  27. int size = sizeof(data) / sizeof(data[0]);
  28. insertionSort(data, size);
  29. cout << "Sorted array in ascending order:\n";
  30. printArray(data, size);
  31. }
Success #stdin #stdout 0s 4180KB
stdin
Standard input is empty
stdout
Sorted array in ascending order:
1 3 4 5 9