我的快速排序未测试

 1 void QuickSort(int arr[], int size)
2 {
3 if(size == 0 || size == 1)
4 {
5 return;
6 }
7 int value = arr[0];
8 int start = 1;
9 int end = size-1;
10 while(true)
11 {
12 if(start>end)
13 return;
14 while(arr[start]<=value&& start<=end)
15 {
16 start++;
17 }
18 while(arr[end]>value && 1 <end)
19 {
20 end--;
21 }
22 if(start<end)//swap
23 {
24 int temp = arr[start];
25 arr[start]=arr[end];
26 arr[end]=temp;
27 }
28 }
29 }

貌似有点问题,用C#写了个版本
View Code
 public static int QuickSortPart(int[] a, int start, int end)
        {


            if (end - start < 1)
                return -1;
            int point = start;
            start = start + 1;

            while (true)
            {
                if (start > end)
                {
                    int temp = a[point];
                    a[point] = a[end];
                    a[end] = temp;
                    return end;
                }
                for (; a[start] <= a[point] && start <= end; start++) ;
                for (; a[end] > a[point] && 1 < end; end--) ;
                if (start < end)
                {
                    int temp = a[start];
                    a[start] = a[end];
                    a[end] = temp;
                }
            }
        }
        public static void QS(int[] a, int start, int end)
        {
            int point = QuickSortPart(a, start, end);
            if (point == -1)
                return;
            QS(a, start, point - 1);
            QS(a, point + 1, end);
        }
        static void Main(string[] args)
        {
            //int[] op1 = new int[] { 3, 6, 4, 2 };
            //QS(op1, 0, op1.Length - 1);

            //Print(op1);}

 

public static int QuickSortPart(int[] a, int start, int end)
        {


            if (end - start <= 1)
                return -1;
            int point = start;
            start = start + 1;

            while (true)
            {
                if (start >= end)
                {
                    int temp = a[point];
                    a[point] = a[end];
                    a[end] = temp;
                    return end;
                }
                for (; a[start] <= a[point] && start <= end; start++) ;
                for (; a[end] > a[point] && 1 < end; end--) ;
                if (start < end)
                {
                    int temp = a[start];
                    a[start] = a[end];
                    a[end] = temp;
                }
            }
        }
        public static void QS(int[] a, int start, int end)
        {
            int point = QuickSortPart(a, start, end);
            if (point == -1)
                return;
            QS(a, start, point - 1);
            QS(a, point + 1, end);
        }
        static void Main(string[] args)
        {
            int[] op1 = new int[] { 3, 6, 4, 2 };
            QS(op1, 0, op1.Length - 1);

            Print(op1);
}

 

posted @ 2012-02-01 19:30  linyliny  阅读(80)  评论(0)    收藏  举报