国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

C#快速排序類

2019-11-18 16:55:11
字體:
供稿:網(wǎng)友

快速排序的基本思想是基于分治策略的。對于輸入的子序列ap..ar,如果規(guī)模足夠小則直接進(jìn)行排序,否則分三步處理:

分解(Divide):將輸入的序列ap..ar劃分成兩個(gè)非空子序列ap..aq和aq+1..ar,使ap..aq中任一元素的值不大于aq+1..ar中任一元素的值。
 
遞歸求解(Conquer):通過遞歸對p..aq和aq+1..ar進(jìn)行排序。
合并(Merge):由于對分解出的兩個(gè)子序列的排序是就地進(jìn)行的,所以在ap..aq和aq+1..ar都排好序后不需要執(zhí)行任何計(jì)算ap..ar就已排好序。
這個(gè)解決流程是符合分治法的基本步驟的。因此,快速排序法是分治法的經(jīng)典應(yīng)用實(shí)例之一。

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.survivalescaperooms.com/tanghuawei/archive/2006/10/19/533711.html


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 霍城县| 宜兰市| 施秉县| 华亭县| 徐闻县| 涞水县| 获嘉县| 平遥县| 正蓝旗| 桑日县| 确山县| 射洪县| 和林格尔县| 天津市| 大方县| 额尔古纳市| 卫辉市| 陵川县| 兴和县| 荔浦县| 临沧市| 安图县| 怀柔区| 南乐县| 渭南市| 香港 | 合肥市| 兴安盟| 商丘市| 策勒县| 阳东县| 浮梁县| 砚山县| 景泰县| 兴义市| 当阳市| 泰顺县| 台江县| 延边| 沙坪坝区| 平利县|