//insert_sort.c
void insertion_sort(int array[], int len)
{
int i = 0;
int j = 0;
int key = 0;
for(j = 1; j < len; j++)
{
key = array[j];
i = j - 1;
while(i >= 0 && array[i] > key)
{
array[i + 1] = array[i];
i--;
}
array[i + 1] = key;
}
return ;
}
//test.c
#include
extern void insertion_sort(int [], int);
int main(int argc, const char *argv[])
{
int i = 0;
int array[10] = {1, 3, 9, 2, 4, 8, 6, 10, 4, 5};
printf("before sort:\n");
for(i = 0; i < 10; i++)
printf("%d, ", array[i]);
printf("\nafter sort:\n");
//insertion_sort test
printf("insertion_sort test:\n");
insertion_sort(array, 10);
for(i = 0; i < 10; i++)
printf("%d, ", array[i]);
printf("\n");
return 0;
}
extern void insertion_sort(int [], int);
int main(int argc, const char *argv[])
{
int i = 0;
int array[10] = {1, 3, 9, 2, 4, 8, 6, 10, 4, 5};
printf("before sort:\n");
for(i = 0; i < 10; i++)
printf("%d, ", array[i]);
printf("\nafter sort:\n");
//insertion_sort test
printf("insertion_sort test:\n");
insertion_sort(array, 10);
for(i = 0; i < 10; i++)
printf("%d, ", array[i]);
printf("\n");
return 0;
}