首 页 | 精品电影 | 音乐天堂 | 在线游戏 | Flash MTV | 三湘书屋 | 幽默笑话 | 三湘图库 | 美女写真 | IT知识库 | QQ贴图 | 加入书签

网页制作网络编程图形图象操作系统冲浪宝典软件教学网络安全认证考试通信技术电子商务业内动态书籍教程原码

最近更新 文章分类 多媒体类 精品软件

本站搜索:
您的位置:三湘时空 -> IT知识库 -> 文章分类 -> C#应用 -> C#快速排序类  
C#快速排序类


文章类别:C#应用 来源: 作者: 发表日期:2006-10-28 字体:[ ]

小游戏 | 在线影院 | 幽默笑话 | 源码下载 | Flash MTV | 音乐试听 | 书屋 | 美女写真

快速排序的基本思想是基于分治策略的。对于输入的子序列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;
}

}
}
http://www.cnblogs.com/tanghuawei/archive/2006/10/19/533711.html

上一篇:两种添加数据到DropDownList控件的方法 下一篇:利用正则表达式来反转一句话,以单词为单位
本栏目热门文章
·C# Socket编程 2006-3-14
·[C#]解决读写包含汉字的txt文件时乱码的问题 2005-10-4
·在C#.net中将查询数据导入EXCEL表输出 2006-3-24
·c#操作word表格 2005-10-4
·使用C#在进度条中显示复制文件的进度 2005-11-13
·C#.Net网络程序开发-Socket篇 2006-3-14
·C#中使用SendMessage 2005-10-4
·利用C#进行AutoCAD的二次开发(一) 2005-10-4
·利用C#进行AutoCAD的二次开发(二) 2005-10-4
·用Visual C#获得计算机名称和IP地址 2005-10-4
新近更新文章
·用 C# 开发 SQL Server 2005 的自定义聚合函数 2006-11-16
·用C#对DBF数据库的操作 2006-11-16
·关于C#中的DateTime类型的细节问题 2006-11-16
·用C#实现由15位身份证号升级到18位的算法 2006-11-16
·C#进制转换 的记录 2006-11-9
·关于C#中的DLLImport 2006-11-9
·Visual C#创建和使用ActiveX组件 2006-11-5
·用C#实现智能设备上的NotifyIcon类 2006-11-5
·在C#中应用哈希表(Hashtable) 2006-10-28
·使用C#开发SmartPhone程序入门 2006-10-28
首 页 | 软件发布 | 广告联系 | 下载帮助 | 意见反馈 | 网站地图
  CopyRight? 2002-2004 WWW.SXSKY.NET? All Rights Reserved
三湘时空 站长QQ:82675303 Email: