插入排序

/*
 * 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 );
}

  

原文地址:https://www.cnblogs.com/lxgeek/p/2185246.html