天天看点

快速排序

__author__ = 'root'


arr_in = [72, 6, 57, 88, 60, 42, 83, 73, 48, 85]


def sort(start, end):
    if end - start < 0:
        return
    i = start
    j = end
    x = arr_in[i]
    while i < j:
        while i < j and arr_in[j] >= x:
            j -= 1
        if i < j:
            arr_in[i] = arr_in[j]
            i += 1

        while i < j and arr_in[i] < x:
            i += 1
        if i < j:
            arr_in[j] = arr_in[i]
            j -= 1
    arr_in[i] = x
    print arr_in
    sort(start, i - 1)
    sort(i + 1, end)

sort(0, len(arr_in) - 1)
print arr_in      
#include <stdio.h>
void quick_sort(int s[], int l, int r)
{
        if(l < r)
        {
                int i = AdjustArray(s, l, r);
                quick_sort(s, l, i-1);
                quick_sort(s, i+1, r);
        }
}

int AdjustArray(int s[], int l, int r)
{
        int i = l, j = r;
        int x = s[l];
        while(i < j)
        {
                while(i < j && s[j] >= x)
                        j --;
                if(i < j)
                {
                        s[i] = s[j];
                        i ++;
                }
                while(i < j && s[i] < x)
                        i++;
                if(i<j)
                {
                        s[j] = s[i];
                        j--;
                }
        }
        s[i] = x;
        return i;
}

main()
{
        int s[] = {72, 6, 57, 88, 60, 42, 83, 73, 48, 85};
        int l = 0;
        int r = 9;
        quick_sort(s, l, r);
        int i;
        for(i=0; i<10;i++)
        {
                printf("%d ",s[i]);

        }
        printf("%s","\n");
        return 0;
}      
上一篇: 快速排序
下一篇: 快速排序