Skip to content

Instantly share code, notes, and snippets.

@shkesar
Created September 22, 2016 08:40
Show Gist options
  • Save shkesar/201fc0f02b35fc708e6352e3c79b648f to your computer and use it in GitHub Desktop.
Save shkesar/201fc0f02b35fc708e6352e3c79b648f to your computer and use it in GitHub Desktop.
Insertion sort
#include <stdio.h>
#include <stdlib.h>
void display_array(int keys[], int len) {
for (int i = 0; i < len; i++) {
printf("%d ", keys[i]);
}
printf("\n");
}
int insertion_sort(int keys[], int len) {
int i, j, save;
for (int i = 1; i < len; i++) {
save = keys[i];
j = i-1;
while (j >= 0 && keys[j] < save) {
keys[j+1] = keys[j];
j--;
}
keys[j+1] = save;
}
return 0;
}
int main(int argc, char **argv) {
int keys[5] = {5,4,3,2,1};
insertion_sort(keys, 5);
display_array(keys, 5);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment