Skip to content

Instantly share code, notes, and snippets.

@gyaikhom
Last active August 29, 2015 14:03
Show Gist options
  • Save gyaikhom/621fa9b48ad38b69ee1e to your computer and use it in GitHub Desktop.
Save gyaikhom/621fa9b48ad38b69ee1e to your computer and use it in GitHub Desktop.
Implementation of Insertion Sorting.
/**
* Copyright 2014 Gagarine Yaikhom (MIT License)
*
* Implementation of Insertion Sorting.
*/
#include <stdio.h>
void print_array(const int *a, int size) {
int i;
for (i = 0; i < size; ++i)
printf("%d ", a[i]);
printf("\n");
}
void insertion_sort(int *a, int size) {
int i, j, t;
for (i = 1; i < size; ++i) {
for (j = i; j > 0 && a[j] < a[j - 1]; --j) {
t = a[j];
a[j] = a[j - 1];
a[j - 1] = t;
}
}
}
int main(void) {
int a[] = {5, 3, 8, 2, 7, 6, 9}, size;
size = sizeof(a) / sizeof(a[0]);
printf("Unsorted: ");
print_array(a, size);
insertion_sort(a, size);
printf(" Sorted: ");
print_array(a, size);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment