c#快速排序

蓝咒 提交于 2020-02-11 09:55:21
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]);
                }
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!