Created
September 22, 2016 08:40
-
-
Save shkesar/201fc0f02b35fc708e6352e3c79b648f to your computer and use it in GitHub Desktop.
Insertion sort
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> | |
#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