Skip to content

Instantly share code, notes, and snippets.

@cengiz-io
Created July 26, 2015 23:07
Show Gist options
  • Select an option

  • Save cengiz-io/9db6b856511675dd5c26 to your computer and use it in GitHub Desktop.

Select an option

Save cengiz-io/9db6b856511675dd5c26 to your computer and use it in GitHub Desktop.
Insertion Sort
#include <stdlib.h>
#include <stdio.h>
void insertion_sort(int *source_array, int number_of_elements) {
int i, j, t;
for (i = 1; i < number_of_elements; i++) {
t = source_array[i];
for (j = i; j > 0 && t < source_array[j - 1]; j--) {
source_array[j] = source_array[j - 1];
}
source_array[j] = t;
}
}
int main() {
int source_array[] = {4, 65, 2, -31, 0, 99, 2, 83, 782, 1};
int number_of_elements = sizeof source_array / sizeof source_array[0];
int i;
for (i = 0; i < number_of_elements; i++) {
printf("%d%s", source_array[i], i == number_of_elements - 1 ? "\n" : " ");
}
insertion_sort(source_array, number_of_elements);
for (i = 0; i < number_of_elements; i++) {
printf("%d%s", source_array[i], i == number_of_elements - 1 ? "\n" : " ");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment