快速排序 v0.1

时间:2021-11-18 20:08:03
/*
 * author:lx
 * breif: 2011.09.22
 * brief: quick-sort
 */


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


void
quick_sort( int *x, int l, int u )
{
        if ( l >= u )
        {
                return;
        }

        int m = l;
        int i;
        for ( i = l+1; i < u; i++ )
        {
                if ( x[i] < x[l] )
                {
                        int temp;
                        m++;
                        temp = x[ m ];
                        x[ m ] = x[ i ];
                        x[ i ] = temp;
                }
        }

        int temp;
        temp = x[ l ];
        x[ l ] = x[ m ];
        x[ m ] = temp;


        quick_sort( x, l, m -1 );
        quick_sort( x, m + 1, u );
}



int
main( void )
{
        int a[8] = { 41, 26, 53, 55, 59, 58, 97, 93 };
        int len = sizeof( a ) / sizeof( int );

        quick_sort( a, 0, len );

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

        exit( 0 );
}