常用排序工具类:标准【正序、倒序】排序算法‘冒泡排序,选择排序,快速排序’

简介:

常用排序工具类:
SortClass 的摘要说明。
对整形数组进行排序
可以重写每个排序算法支持多类型
注意:数组、对象,为传地址指针的形式
执行方法后会便改原始数组内容。

支持:
1、冒泡排序
2、选择排序
3、快速排序

None.gif using System;
None.gif
None.gif namespace 排序算法
ExpandedBlockStart.gif ContractedBlock.gif dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 排序模式
ExpandedSubBlockEnd.gif
/// </summary>

InBlock.gifpublic enum SortTypeEnum
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif ASC, //正序 A-Z
InBlock.gif
DESC //到序 Z-A
ExpandedSubBlockEnd.gif
}

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// SortClass 的摘要说明。
InBlock.gif
/// 对整形数组进行排序
InBlock.gif
/// 可以重写每个排序算法支持多类型
InBlock.gif
/// 注意:数组、对象,为传地址指针的形式
InBlock.gif
/// 执行方法后会便改原始数组内容。
InBlock.gif
///
InBlock.gif
/// 支持:
InBlock.gif
/// 1、冒泡排序
InBlock.gif
/// 2、选择排序
InBlock.gif
/// 3、快速排序
ExpandedSubBlockEnd.gif
/// </summary>

InBlock.gifpublic class SortClass
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifprivate SortTypeEnum oSortType = SortTypeEnum.ASC;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 构造
ExpandedSubBlockEnd.gif
/// </summary>

ExpandedSubBlockStart.gifContractedSubBlock.gifpublic SortClass()dot.gif{;}
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 构造
InBlock.gif
/// </summary>
ExpandedSubBlockEnd.gif
/// <param name="ste">排序模式</param>

InBlock.gifpublic SortClass(SortTypeEnum ste)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifthis.oSortType = ste;
ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 交换函数
InBlock.gif
/// </summary>
InBlock.gif
/// <param name="a">第一</param>
ExpandedSubBlockEnd.gif
/// <param name="b">第二</param>

InBlock.gifprotected void Swap(ref int a,ref int b)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifint c = a; a = b; b = c;
ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 排序模式
ExpandedSubBlockEnd.gif
/// </summary>

InBlock.gifpublic SortTypeEnum SortType
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifget
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifreturn this.oSortType;
ExpandedSubBlockEnd.gif }

InBlock.gifset
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifthis.oSortType = value;
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 冒泡排序
InBlock.gif
/// </summary>
ExpandedSubBlockEnd.gif
/// <param name="a">排序数组</param>

InBlock.gifpublic void BubbleSort(int[] a)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifint i = a.Length-1;
InBlock.gifwhile(i>=0)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.giffor(int j=0;j<a.Length-1;j++)
InBlock.gifswitch(oSortType)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifcase SortTypeEnum.ASC:
InBlock.gifif(a[j]>a[j+1])
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif Swap(ref a[j],ref a[j+1]);
ExpandedSubBlockEnd.gif }

InBlock.gifbreak;
InBlock.gifcase SortTypeEnum.DESC:
InBlock.gifif(a[j]<a[j+1])
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif Swap(ref a[j],ref a[j+1]);
ExpandedSubBlockEnd.gif }

InBlock.gifbreak;
ExpandedSubBlockEnd.gif }

InBlock.gif i--;
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 选择排序
InBlock.gif
/// </summary>
ExpandedSubBlockEnd.gif
/// <param name="a">排序数组</param>

InBlock.gifpublic void SelectionSort(int[] a)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.giffor(int i=0;i<a.Length-1;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.giffor(int j=a.Length-1;j>=i+1;j--)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifswitch(oSortType)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifcase SortTypeEnum.ASC:
InBlock.gifif(a[i]>a[j])
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif Swap(ref a[i],ref a[j]);
ExpandedSubBlockEnd.gif }

InBlock.gifbreak;
InBlock.gifcase SortTypeEnum.DESC:
InBlock.gifif(a[i]<a[j])
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif Swap(ref a[i],ref a[j]);
ExpandedSubBlockEnd.gif }

InBlock.gifbreak;
ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 快速排序递归子过程
InBlock.gif
/// </summary>
InBlock.gif
/// <param name="a">排序数组</param>
InBlock.gif
/// <param name="iLo">低位</param>
ExpandedSubBlockEnd.gif
/// <param name="iHi">高位</param>

InBlock.gifprivate void QuickSortRecursion(int[] a,int iLo,int iHi)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifint lo = iLo;
InBlock.gifint hi = iHi;
InBlock.gifint mid = a[(int)((lo+hi) >> 1)];
InBlock.gifdo
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifswitch(oSortType)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifcase SortTypeEnum.ASC:
InBlock.gifwhile(a[lo]<mid) lo ++;
InBlock.gifwhile(a[hi]>mid) hi --;
InBlock.gifbreak;
InBlock.gifcase SortTypeEnum.DESC:
InBlock.gifwhile(a[lo]>mid) lo ++;
InBlock.gifwhile(a[hi]<mid) hi --;
InBlock.gifbreak;
ExpandedSubBlockEnd.gif }

InBlock.gifif(lo<=hi)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif Swap(ref a[lo],ref a[hi]);
InBlock.gif lo++;
InBlock.gif hi--;
ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockEnd.gif }
while(lo<hi);
InBlock.gifif(hi>iLo)QuickSortRecursion(a,iLo,hi);
InBlock.gifif(lo<iHi)QuickSortRecursion(a,lo,iHi);
ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 快速排序
InBlock.gif
/// </summary>
ExpandedSubBlockEnd.gif
/// <param name="a">排序数组</param>

InBlock.gifpublic void QuickSort(int[] a)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif QuickSortRecursion(a,0,a.Length-1);
ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}

None.gif

测试样例:
None.gif // 数组
ExpandedBlockStart.gifContractedBlock.gif
int[] test = new int[] dot.gif {9,8,7,4,3,35,4,5,3,21,1,4,57,2,123,4,0};
None.gif // 实例化
None.gif
SortClass sc = new SortClass();
None.gif // 排序模式
None.gif
sc.SortType = SortTypeEnum.DESC;
None.gif // 冒泡排序
None.gif
sc.BubbleSort(test);
None.gif // 选择排序
None.gif
sc.SelectionSort(test);
None.gif // 快速排序
None.gif
sc.QuickSort(test);
None.gif // 输出结果
None.gif
textBox1.Text = "";
None.gif for( int i=0;i<test.Length;i++)
ExpandedBlockStart.gif ContractedBlock.gif dot.gif {
InBlock.gif textBox1.Text += test[i].ToString() +"\r\n";
ExpandedBlockEnd.gif }



本文转自suifei博客园博客,原文链接http://www.cnblogs.com/Chinasf/archive/2005/05/19/159182.html,如需转载请自行联系原作者
相关文章
|
1月前
|
人工智能 算法 测试技术
【数学】【排序】【C++算法】3027人员站位的方案数
【数学】【排序】【C++算法】3027人员站位的方案数
|
1月前
|
人工智能 算法 BI
【优选算法专栏】专题十八:BFS解决拓扑排序(一)
【优选算法专栏】专题十八:BFS解决拓扑排序(一)
20 0
|
1月前
|
算法
【优选算法专栏】专题十八:BFS解决拓扑排序--前言
【优选算法专栏】专题十八:BFS解决拓扑排序--前言
22 1
|
7天前
|
算法 前端开发
前端算法之快速排序
前端算法之快速排序
12 0
|
3天前
|
算法
常见的算法排序(2)
常见的算法排序(2)
11 3
|
3天前
|
算法 搜索推荐 索引
数据结构与算法 排序(下)
数据结构与算法 排序(下)
10 1
|
3天前
|
缓存 算法 搜索推荐
数据结构与算法 排序(上)
数据结构与算法 排序(上)
9 0
|
5天前
|
算法 调度
【问题探讨】基于非支配排序的蜣螂优化算法NSDBO求解微电网多目标优化调度研究
【问题探讨】基于非支配排序的蜣螂优化算法NSDBO求解微电网多目标优化调度研究
|
7天前
|
算法 前端开发 搜索推荐
前端算法之选择排序
前端算法之选择排序
10 0
|
7天前
|
搜索推荐 C语言
【C语言/数据结构】排序(归并排序|计数排序|排序算法复杂度)
【C语言/数据结构】排序(归并排序|计数排序|排序算法复杂度)
11 0