using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace QuickSortByCsharp
{
    class Program
    {
        public void swap(ref int a, ref int b)
        {
            int temp = 0;
            temp = a;
            a = b;
            b = temp;
        }        public void QuickSort(int[] Array,int l,int r) 
        {
            int i = 0, j = 0, s = 0;
            if (l < r)
            {
                i = l;
                j = r;
                s = Array[l];                while (true)
                {
                    while (i + 1 < r + 1 && Array[++i] < s) ;
                    while (j - 1 > -1 && Array[--j] > s) ;
                    if (i >= j)
                    {
                        break;
                    }
                    else
                    {
                        swap(ref Array[i], ref Array[j]);
                    }
                }                Array[l] = Array[j];
                Array[j] = s;                QuickSort(Array, l, j - 1);
                QuickSort(Array, j + 1, r);
            }
        }        static void Main(string[] args)
        {
            Program p = new Program();
            int[] NewArr = new int[6] {7,5,6,4,2,9 };
            p.QuickSort(NewArr,0,NewArr.Length-1);
            foreach (int i in NewArr)
                Console.WriteLine(i.ToString()+" ");        }
    }
}

解决方案 »

  1.   

    使用Array.Sort就是快速排序了。没必要自已重写了。
      

  2.   

    给你一个快排的算法参考一下:int[] newArr = { 7, 5, 6, 4, 2, 9 };
    QuickSort(newArr, 0, newArr.Length-1);
    public static void QuickSort(int[] array, int left, int right)
    {
    do
    {
    int index = left;
    int num2 = right;
    int num3 = array[index + ((num2 - index) >> 1)];
    do
    {
    while ((index < array.Length) && (num3 > array[index]))
    {
    index++;
    }
    while ((num2 >= 0) && (num3 < array[num2]))
    {
    num2--;
    }
    if (index > num2)
    {
    break;
    }
    if (index < num2)
    {
    int num4 = array[index];
    array[index] = array[num2];
    array[num2] = num4;
    }
    index++;
    num2--;
    }
    while (index <= num2);
    if ((num2 - left) <= (right - index))
    {
    if (left < num2)
    {
    QuickSort(array, left, num2);
    }
    left = index;
    }
    else
    {
    if (index < right)
    {
    QuickSort(array, index, right);
    }
    right = num2;
    }
    }
    while (left < right);
    }
      

  3.   


    上面那个是我写的简化版的程序,不需要像你这样这么麻烦的,我用同样的思路,在VC上面调试是没问题的!
    如下:
    #include "stdafx.h"
    #include <iostream>using namespace std;void Swap(int &a,int &b)
    {
     int temp;
     temp =a;
     a=b;
     b=temp;
    }void QuickSort(int *Array,int l,int r)
    {
    int i,j,s;
    if(l<r)
    {
     i=l;
     j=r+1;
     s=Array[l];  while(true)
     {
      while(i+1<r+1 && Array[++i]<s);
      while(j-1>-1 && Array[--j]>s);
      if(i>=j)
      break;
          Swap(Array[i],Array[j]);
     }
     Array[l]=Array[j];
     Array[j]=s;  QuickSort(Array,l,j-1);
     QuickSort(Array,j+1,r);
    }}int _tmain(int argc, _TCHAR* argv[])
    {
    int Arr[5]={8,4,9,6,10};
    QuickSort(Arr,0,4);
    for(int i=0;i<5;i++)
    {
     cout<<Arr[i]<<" ";
    }
    return 0;
    }