quick sort O(logn)

#include <stdio.h>

int partition(int* p, int low, int high);
void Qsort(int* p, int low, int high);
void quickOrder(int* p, int len);
int main()
{
        int array[10];
        int len;
        len = input(array);

        quickOrder(array, len);
        for(int i =0; array[i] != '\0'; i++) {
                printf("number is:%d\r\n", array[i]);
        }
        return 0;
}
int input(int* array)
{
        int n;
        int i = 0;
        while(1) {
                scanf("%d", &n);
                if(n == -1) break;
                *array = n;
                array++;
                i++;
        }

        return i;
}

int partition(int* p, int low, int high)
{
        int pivotkey = *(p+low);
        while(low < high) {
                while(low < high && *(p+high) >= pivotkey)  --high;
                *(p+low) = *(p+high);
                while(low < high && *(p+low) <= pivotkey) ++low;
                *(p+high) = *(p+low);

        }
        *(p+low) = pivotkey;
        return low;

}
void Qsort(int* p, int low, int high)
{
        if(low < high) {
                int pivotloc =  partition(p, low, high);
                Qsort(p, low, pivotloc-1);
                Qsort(p, pivotloc+1,high);
        }
}
void quickOrder(int* p, int len)
{
        Qsort(p, 0, len-1);
}

 

posted @ 2013-03-12 15:44  通杀  阅读(146)  评论(0编辑  收藏  举报