C#几种排序算法

时间:2021-05-20

作者:Sabine【导读】本文介绍了C#的四种排序算法:冒泡排序、选择排序、插入排序和希尔排序

 冒泡排序

usingSystem;

namespaceBubbleSorter

{publicclassBubbleSorter

{publicvoidSort(int[]list)

{inti,j,temp;

booldone=false;

j=1;

while((j<list.Length)&&(!done))

{done=true;

for(i=0;i<list.Length-j;i++)

{

if(list[i]>list[i+1])

{

done=false;

temp=list[i];

list[i]=list[i+1];

list[i+1]=temp;

}}

j++;}

}}

publicclassMainClass

{publicstaticvoidMain()

{

int[]iArrary=newint[]{1,5,13,6,10,55,99,2,87,12,34,75,33,47};

BubbleSortersh=newBubbleSorter();

sh.Sort(iArrary);

for(intm=0;m<iArrary.Length;m++)

Console.Write("{0}",iArrary[m]);

Console.WriteLine();

}}

}




选择排序

usingSystem;



namespaceSelectionSorter

{publicclassSelectionSorter

{privateintmin;

publicvoidSort(int[]list)

{for(inti=0;i<list.Length-1;i++)

{min=i;

for(intj=i+1;j<list.Length;j++)

{if(list[j]<list[min])

min=j;

}

intt=list[min];

list[min]=list[i];

list[i]=t;

}}

}

publicclassMainClass

{publicstaticvoidMain()

{

int[]iArrary=newint[]{1,5,3,6,10,55,9,2,87,12,34,75,33,47};

SelectionSorterss=newSelectionSorter();

ss.Sort(iArrary);

for(intm=0;m<iArrary.Length;m++)

Console.Write("{0}",iArrary[m]);

Console.WriteLine();

}}

}




插入排序

usingSystem;

namespaceInsertionSorter

{publicclassInsertionSorter

{publicvoidSort(int[]list)

{for(inti=1;i<list.Length;i++)

{intt=list[i];

intj=i;

while((j>0)&&(list[j-1]>t))

{list[j]=list[j-1];

--j;

}

list[j]=t;}

}

}

publicclassMainClass

{publicstaticvoidMain()

{

int[]iArrary=newint[]{1,13,3,6,10,55,98,2,87,12,34,75,33,47};

InsertionSorterii=newInsertionSorter();

ii.Sort(iArrary);

for(intm=0;m<iArrary.Length;m++)

Console.Write("{0}",iArrary[m]);

Console.WriteLine();

}}

}




希尔排序

 希尔排序是将组分段,进行插入排序.

usingSystem;

namespaceShellSorter

{

publicclassShellSorter

{

publicvoidSort(int[]list)

{

intinc;

for(inc=1;inc<=list.Length/9;inc=3*inc+1);

for(;inc>0;inc/=3)

{

for(inti=inc+1;i<=list.Length;i+=inc)

{

intt=list[i-1];

intj=i;

while((j>inc)&&(list[j-inc-1]>t))

{

list[j-1]=list[j-inc-1];

j-=inc;

}

list[j-1]=t;

}}

}}

publicclassMainClass

{publicstaticvoidMain()

{

int[]iArrary=newint[]{1,5,13,6,10,55,99,2,87,12,34,75,33,47};

ShellSortersh=newShellSorter();

sh.Sort(iArrary);

for(intm=0;m<iArrary.Length;m++)

Console.Write("{0}",iArrary[m]);

Console.WriteLine();

}}

}






快速排序

usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;

namespaceSoloDataStructure
{
classMyQuickSort
{
///<summary>
///快速排序算法
///</summary>
///快速排序为不稳定排序,时间复杂度O(nlog2n),为同数量级中最快的排序方法
///<paramname="arr">划分的数组</param>
///<paramname="low">数组低端上标</param>
///<paramname="high">数组高端下标</param>
///<returns></returns>
staticintPartition(int[]arr,intlow,inthigh)
{
//进行一趟快速排序,返回中心轴记录位置
//arr[0]=arr[low];
intpivot=arr[low];//把中心轴置于arr[0]
while(low<high)
{
while(low<high&&arr[high]>=pivot)
--high;
//将比中心轴记录小的移到低端
Swap(refarr[high],refarr[low]);
while(low<high&&arr[low]<=pivot)
++low;
Swap(refarr[high],refarr[low]);
//将比中心轴记录大的移到高端
}
arr[low]=pivot;//中心轴移到正确位置
returnlow;//返回中心轴位置
}
staticvoidSwap(refinti,refintj)
{
intt;
t=i;
i=j;
j=t;
}
staticvoidQuickSort(int[]arr,intlow,inthigh)
{
if(low<high-1)//当arr[low,high]为空或只一个记录无需排序
{
intpivot=Partition(arr,low,high);
QuickSort(arr,low,pivot-1);
QuickSort(arr,pivot+1,high);

}
}
staticvoidMain(string[]args)
{
int[]arr=newint[]{54,62,99,14,28,1,8,77,99,3,110};
QuickSort(arr,0,arr.Length-1);
Console.Write("DataAfterQuickSort:");
foreach(intiinarr)
{
Console.Write(i+",");
}
Console.ReadLine();
}
}
}

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章