Skip to content

Instantly share code, notes, and snippets.

@easonhan007
Created September 5, 2014 14:00
Show Gist options
  • Save easonhan007/70d6d8fb6e2ade028930 to your computer and use it in GitHub Desktop.
Save easonhan007/70d6d8fb6e2ade028930 to your computer and use it in GitHub Desktop.
how to implement insert sort using c
#include <stdio.h>
int main() {
int arr[] = {1, 8, 9, 4, 8, 7, 2, 3, 4, 90, 34};
int len = sizeof(arr) / sizeof(arr[0]);
int i;
for(i = 1; i < len - 1; i++) {
int key = arr[i];
int j = i - 1;
while(arr[j] > key && j >=0) {
arr[j + 1] = arr[j];
j--;
}
arr[j+1] = key;
}
for(i = 0; i < len - 1; i++)
printf("%d ", arr[i]);
printf("\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment