fork download
  1. // Insertion sort in C
  2.  
  3. #include <stdio.h>
  4.  
  5. // Function to print an array
  6. void printArray(int array[], int size) {
  7. for (int i = 0; i < size; i++) {
  8. printf("%d ", array[i]);
  9. }
  10. printf("\n");
  11. }
  12.  
  13. void insertionSort(int array[], int size) {
  14. for (int step = 1; step < size; step++) {
  15. int key = array[step];
  16. int j = step - 1;
  17.  
  18.  
  19. while (j >=0 && key < array[j]) {
  20. array[j + 1] = array[j];
  21. --j;
  22. }
  23. array[j + 1] = key;
  24. }
  25. }
  26.  
  27. // Driver code
  28. int main() {
  29. int data[] = {1000};
  30. int size = sizeof(data) / sizeof(data[0]);
  31. insertionSort(data, size);
  32. printf("Sorted array in ascending order:\n");
  33. printArray(data, size);
  34. }
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
Sorted array in ascending order:
1000