首页 > 代码库 > 算法(一):插入排序

算法(一):插入排序

root@ubuntu:/mnt/shared/appbox/issort# cat issort.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

int issort(int *array, int num)
{
        int i, j, tmp;

        for(j=1; j<num; j++)
        {
                tmp = array[j];
                i = j - 1;
                while( i >= 0 && array[i] > tmp)
                {
                        array[i+1] = array[i];
                        i--;

                }
                array[i+1] = tmp;
        }
        return 0;
}

int main(int argc, char *argv[])
{
        int i;
        int a[10] = {1,3,5,7,9,2,4,6,8};

        issort(a, 9);

        for(i=0; i<9; i++)
        {
                printf("%d,", a[i]);
        }

        printf("\n");

        return 0;
}

算法(一):插入排序