插入排序

/*
 * author:lx
 * date:2011.09.22
 * brief: insertion-sort
 */

#include <stdio.h>
#include <stdlib.h>

void 
insertion_sort( int *p, int len )
{
        int j = 1;
        int key = 0;
        int i;

        for ( ; j < len; j++ )
        {
                key = p[j];

                i = j - 1;

                while ( i >= 0  && p[i] > key )
                {
                        p[i+1] = p[i];
                        i -= 1;
                } 

                p[ i+1 ] = key;
        }
}


int
main( void )
{
        int a[9] = { 4, 5, 7, 0, 1, 8 , 11, 123, -1 };
        int len = sizeof( a ) / sizeof( int );

        insertion_sort( a, len );

        int i;
        for ( i  = 0; i < len; i++ )
                printf( "%d\n", a[i] );
        exit( 0 );
}

  

posted @ 2011-09-22 16:17  lxgeek  阅读(138)  评论(0编辑  收藏  举报