快速排序

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

#define L 10

int array[L] = {0};


int Partition ( int *R, int low, int high ) {
    int pot = high;
    int i = low;
    while ( i < pot ) {
        if ( R[i] > R[high] ) {
            int tmp = R[i];
            R[i--] = R[--pot];
            R[pot] = tmp;
        }
        i++;
    }
    int tmp = R[high];
    R[high] = R[pot];
    R[pot] = tmp;
    return pot;    
}

void QuickSort ( int *R, int low, int high ) {
    if ( low < high ) {
        int pot = Partition ( R, low, high );
        QuickSort ( R, low , pot - 1 );
        QuickSort (R, pot, high );
    }
}


int main() {
    for ( int i = 0; i < L; i++ ) {
        array[i] = rand()%100;
        printf("%d ", array[i]);
    }
    printf ("\n");

    QuickSort ( array, 0, L-1 );

    for ( int i = 0; i < L; i++ ) {
        printf("%d ", array[i]);
    }
    printf ("\n");
}
posted @ 2012-11-28 10:44  tsubasa_wp  阅读(145)  评论(0编辑  收藏  举报