【算法】C#快速排序类
2004-12-16 18:51
253 查看
快速排序的基本思想是基于分治策略的。对于输入的子序列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;
}
}
}
分解(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;
}
}
}
相关文章推荐
- 【算法】C#快速排序类(转载)
- 【算法】C#快速排序类
- 【算法】C#快速排序类
- C#实现的18位身份证格式验证算法[转载]
- C#实现的18位身份证格式验证算法
- 一个简单的统计图像主颜色的算法(C#源代码)
- 【原创】机器学习之PageRank算法应用与C#实现(2)球队排名应用与C#代码
- 关于一道面试题,使用C#实现字符串反转算法
- c# 应用NPOI 获取Excel中的图片,保存至本地的算法
- C#算法之全排列递归算法实例讲解
- C#压缩图片算法
- 算法-排序(C#)
- 经典算法-C#四种排序算法
- 【C#】洗牌算法、利用delegate自定义list的sort排序方法、summary注释
- C#解决Socket丢包,段包,粘包算法
- C#实现用欧几里德算法、连续整数检测算法、公因数算法求两个非负整数的最大公约数
- C#开发WPF/Silverlight动画及游戏系列教程(Game Tutorial):(十)斜度α地图的构造及算法
- C#算法(三)
- 小小c#算法题 - 8 - 归并排序 (Merging Sort)
- 30种图像动画特效算法(C#多线程版)(中)