fueanta

Insertion Sorting.

Oct 14th, 2016
149
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Problem : Introduction to Insertion Sorting.
  2. // AUTHOR : fueanta
  3. // Written : 14-Oct-16
  4.  
  5. #include "cstdio"
  6.  
  7. void ins_sort(int *, int);
  8.  
  9. void main() {
  10.     int arr[] = { 5, 3, 7, 2, 4, 5, 1 };
  11.     printf("\n"); ins_sort(arr, (sizeof(arr) / 4));
  12.     for (int i = 0; i < (sizeof(arr) / 4); i++) {
  13.         printf("|%d|", arr[i]);
  14.     }
  15.     printf("\n\n");
  16. }
  17.  
  18. void ins_sort(int arr[], int n) {
  19.     for (int i = 1; i <= n - 1; i++) {
  20.         int LOC = i;
  21.         int VAL = arr[LOC];
  22.         while (LOC > 0 && arr[LOC - 1] > VAL) {
  23.             arr[LOC] = arr[LOC - 1];
  24.             LOC--;
  25.         }
  26.         arr[LOC] = VAL;
  27.     }
  28. }
RAW Paste Data