中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档 | 网通镜像
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > .NET > 临时文章
【算法】C#快速排序类
作者:未知 时间:2005-07-27 21:29 出处:CSDN 责编:chinaitpower
              摘要:【算法】C#快速排序类

快速排序的基本思想是基于分治策略的。对于输入的子序列ap..ar,如果规模足够小则直接进行排序,否则分三步处理:

分解(Divide):将输入的序列ap..ar划分成两个非空子序列ap..aq和aq+1..ar,使ap..aq中任一元素的值不大于aq+1..ar中任一元素的值。 
递归求解(Conquer):通过递归对p..aq和aq+1..ar进行排序。 
合并(Merge):由于对分解出的两个子序列的排序是就地进行的,所以在ap..aq和aq+1..ar都排好序后不需要执行任何计算ap..ar就已排好序。 
这个解决流程是符合分治法的基本步骤的。因此,快速排序法是分治法的经典应用实例之一。

using System;

namespace VcQuickSort
{
 /// <summary>
 /// ClassQuickSort 快速排序。
 /// 范维肖
 /// </summary>

 public class QuickSort
 {
  public QuickSort()
  {
  }

  private void Swap(ref int i,ref int j)
  //swap two integer
  {
   int t;
   t=i;
   i=j;
   j=t;
  }
  
  public void Sort(int [] list,int low,int high)
  {
   if(high<=low)
   {
    //only one element in array list
    //so it do not need sort
    return;
   }
   else if (high==low+1)
   {
    //means two elements in array list
    //so we just compare them

    if(list[low]>list[high])
    {
     //exchange them
     Swap(ref list[low],ref list[high]);
     return;
    }
   }
   //more than 3 elements in the arrary list
   //begin QuickSort

   myQuickSort(list,low,high);
  }

  public void myQuickSort(int [] list,int low,int high)
  {
   if(low<high)
   {
    int pivot=Partition(list,low,high);
    myQuickSort(list,low,pivot-1);
    myQuickSort(list,pivot+1,high);
   }
  }

  private int Partition(int [] list,int low,int high)
  {
   //get the pivot of the arrary list
   int pivot;
   pivot=list[low];
   while(low<high)
   {
    while(low<high && list[high]>=pivot)
    {
     high--;
    }
    if(low!=high)
    {
     Swap(ref list[low],ref list[high]);
     low++;
    }
    while(low<high && list[low]<=pivot)
    {
     low++;
    }
    if(low!=high)
    {
     Swap(ref list[low],ref list[high]); 
     high--;
    }
   }
   return low;
  }

 }
}


关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有