动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
   程序开发 → 阅读文章

 C#算法(四)快速排序

作者来源: 
阅读 数 571 人次 , 2006-5-10 10:37:00 

   前面我已经推出了三种排序的算法,比较简单。今天我又写了快速排序的算法。希望多多指教。具体的思想,我不做答了。前人的经验。
  
  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
  
 本文Tags算法  C#  
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:C#算法(三)希尔排序
· 下一篇:如何在C#中读写INI文件
· Windows XP隔离组件和并列共享
· Delphi:程序间相互通讯问题的解决
· Ruby程序设计语言快速入门之安装Ruby
· 面向对象编程:Java的简单数据类型
· 更好的控制客户需求


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077