Created
September 5, 2014 14:00
-
-
Save easonhan007/70d6d8fb6e2ade028930 to your computer and use it in GitHub Desktop.
how to implement insert sort using c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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