fork download
  1. #include <stdio.h>
  2.  
  3. void insertionSort(int arr[] , int n)
  4. {
  5. int i, j, key;
  6.  
  7. for (i = 1; i < n; i++)
  8. {
  9. key = arr[i];
  10. j = i - 1;
  11.  
  12. while (j >=0 && arr[j] > key)
  13. {
  14. arr[j + 1] = arr[j];
  15. j = j - 1;
  16.  
  17. }
  18. arr[j + 1] = key;
  19. }
  20. }
  21. // A utility function to print an array of size n
  22. void printArray(int arr[], int n)
  23. {
  24. int i;
  25. for (i = 0; i < n; i++)
  26. printf("%d ", arr[i]);
  27. printf("\n");
  28. }
  29.  
  30. /* Driver program to test insertion sort */
  31. int main()
  32. {
  33. int arr[] = { 12, 11, 13, 5, 6 };
  34. int n = sizeof(arr) / sizeof(arr[0]);
  35.  
  36. insertionSort(arr, n);
  37. printArray(arr, n);
  38.  
  39. return 0;
  40. }
Success #stdin #stdout 0.01s 5300KB
stdin
Standard input is empty
stdout
5 6 11 12 13