快速排序

参考源:http://www.cnblogs.com/morewindows/archive/2011/08/13/2137415.html
__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;
}

 

posted on 2013-12-05 16:36  雨渐渐  阅读(181)  评论(0编辑  收藏  举报

导航