c#快速排序

using System;
/*
描写:C#实现快速排序算法
创建日期:2006/05/08
*/
namespace ConsoleApplication1
{
    
class DataStructDemo
    {
        
static void swap(ref int a,ref int b)
        {
            
int temp;
            temp
=a;
            a
=b;
            b
=temp;
        }
        
static void sort(int[] arr,int left,int right)
        {
            
int i,j,s;

            
if(left < right)
            {
                i
=left - 1;
                j
=right + 1;
                s
=arr[(i + j) / 2];
                
while(true)
                {
                    
while(arr[++i]<s);
                    
while(arr[--j]>s);
                    
if(i>=j)
                        
break;
                    swap(
ref arr[i],ref arr[j]);
                }
                sort(arr,left,i
-1);
                sort(arr,j
+1,right);
            }
        }
            [STAThread]
        
static void Main(string[] args)
        {
                
int[] arr={2,4,65,76,87,90,56,89,78};
                sort(arr,
0,arr.Length-1);
                 Console.WriteLine(
"            Quick Sort Test!!!");
                
for(int i=0;i<arr.Length;i++)
                {
                    Console.WriteLine(arr[i]);
                }
        }
    }
}

 

posted @ 2008-05-13 12:44  debugzhu  阅读(1585)  评论(2编辑  收藏  举报