前面我已经推出了三种排序的算法,比较简单。今天我又写了快速排序的算法。希望多多指教。具体的思想,我不做答了。前人的经验。
using system;
namespace quicksorter
{
public class quicksorter
{
private void swap(ref int l,ref int r)
{
int s;
s=l;
l=r;
r=s;
}
public void sort(int [] list,int low,int high)
{
int pivot;
int l,r;
int mid;
if(high<=low)
return;
else if(high==low+1)
{
if(list[low]>list[high])
swap(ref list[low],ref list[high]);
return;
}
mid=(low+high)>>1;
pivot=list[mid];
swap(ref list[low],ref list[mid]);
l=low+1;
r=high;
do
{
while(l<=r&&list[l]<pivot)
l++;
while(list[r]>=pivot)
r--;
if(l<r)
swap(ref list[l],ref list[r]);
}while(l<r);
list[low]=list[r];
list[r]=pivot;
if(low+1<r)
sort(list,low,r-1);
if(r+1<high)
sort(list,r+1,high);
}
}
public class mainclass
{
public static void main()
{
int[] iarrary=new int[]{1,5,3,6,10,55,9,2,87,12,34,75,33,47};
quicksorter q=new quicksorter();
q.sort(iarrary,0,13);
for(int m=0;m<=13;m++)
console.writeline("{0}",iarrary[m]);
}
}
}
已经编译通过,运行环境:windows xp vc#.net 7.0
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器