Source

woocode / c / algorithms / insertion_1.c

Full commit
/*
* =====================================================================================
*
*       Filename:  insertion_1.c
*
*    Description:  insertion_1
*
*        Version:  1.0
*        Created:  06/05/2012 10:56:30 PM
*       Revision:  none
*       Compiler:  gcc
*
*         Author:  Yang Xiaoyong (), yan5yang@gmail.com
*   Organization:
*
* =====================================================================================
*/
#include <stdlib.h>
#include <stdio.h>

#define LEN 5
int a[LEN] = {10, 5, 2, 4, 7};

void insertion_sort(void) {
    int i, j, key;
    for (j=1; j<LEN; j++) {
        printf("%d, %d, %d, %d, %d\n", a[0], a[1], a[2], a[3], a[4]);
        key = a[j];
        i = j - 1;
        while (i >= 0 && a[i] > key) {
            a[i+1] = a[i];
            i--;
        }
        a[i+1] = key;
    }
    printf("%d, %d, %d, %d, %d\n", a[0], a[1], a[2], a[3], a[4]);
}

int main(void) {
    insertion_sort();
    return 0;
}