【算法】C#快速排序类
来源:互联网 发布:上海巨人网络校招 编辑:程序博客网 时间:2024/05/17 06:44
快速排序的基本思想是基于分治策略的。对于输入的子序列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;
}
}
}
- 【算法】C#快速排序类
- 【算法】C#快速排序类
- C#算法 快速排序
- 快速排序算法c#
- C#快速排序算法
- C#快速排序算法
- 【算法】C#快速排序类(转载)
- C#实现快速排序算法
- C#快速排序算法实现
- 快速排序算法c#版
- C#算法-------(五)快速排序
- 快速排序算法-c#版
- c# 快速排序算法实现
- C#实现快速排序算法
- 【快速排序算法-C#实现】
- C#泛型快速排序算法
- 快速排序算法(C#实现)
- c#冒泡排序算法和快速排序算法
- javascript实现随机产生数字与字母的组合
- 从今天开始,一个线程诞生了
- 制作最清晰缩略图的完整类
- 我要问James Gosling先生关于JDK对AOP技术支持的问题。
- JDBC Review 2
- 【算法】C#快速排序类
- 访问局域网资源
- 认识ASP.NET配置文件Web.config
- 关于asp.net无法访问UNC路径文件的解决
- [转] PHP开发框架总结
- 经理人所必需具备的素质
- 在ASP.NET中创建安全的web站点
- 什么是web.config
- 常用易懂的精品源码