代码
static int Partition(int[] array, int low, int high) 
        {

            
int pivot = array[low];
            
while (low < high) 
            {
                
while (low < high && array[high] >= pivot) 
                {
                    high
--;
                }
                
if (low != high)
                {
                    swap(
ref array[low], ref array[high]);
                }


                
while (low < high && array[low] <= pivot)
                {
                    low
++;
                }
                
if (high != low) 
                {
                    swap(
ref array[high], ref array[low]);
                }
  

            }
   
            
return low;
        }

        
static void QSort(int[] array, int low, int high) 
        {
            
if (low < high)
            {
                
int pivotloc = Partition(array, low, high);
                QSort(array, low, pivotloc 
- 1);
                QSort(array, pivotloc 
+ 1, high);
            }
        }

        
static void QuickSort(int[] array) 
        {
            QSort(array, 
0, array.Length - 1);
        }



        
static void swap(ref int a, ref int b) 
        {
            
int temp = a;
            a 
= b;
            b 
= temp;
        }
        
        
static void Main(string[] args)
        {
            
int[] a = new int[] { 6,2,5,3,1,4,7 };
            QuickSort(a);

        }


posted on 2010-06-03 22:37  havies  阅读(336)  评论(0编辑  收藏  举报