-
插入排序
基本思想:
每次将一个待排序的数据元素,插入到前面已经排好序的数列中的适当位置,使数列依然有序;直到待排序数据元素全部插入完为止。
实例代码:
public class InsertionSorter
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
public void Sort(int [] list)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
for(int i=1;i<list.Length;i++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int t=list[i];
int j=i;
![](/Images/OutliningIndicators/InBlock.gif)
while((j>0)&&(list[j-1]>t))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
list[j]=list[j-1];
--j;
}
![](/Images/OutliningIndicators/InBlock.gif)
list[j]=t;
}
}
}
![](/Images/OutliningIndicators/None.gif)
public class MainClassTest3
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
public static void Main()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
int[] iArrary=new int[]
{1,13,3,6,10,55,98,2,87,12,34,75,33,47};
InsertionSorter ii=new InsertionSorter();
ii.Sort(iArrary);
for (int m = 0; m < iArrary.Length; m++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine();
Console.Write("{0}", iArrary[m]);
}
![](/Images/OutliningIndicators/InBlock.gif)
Console.WriteLine();
}
}
![](/Images/OutliningIndicators/None.gif)
运行结果:
![](/images/cnblogs_com/abcdwxc/d2.GIF)
-
相关阅读:
【prufer编码】BZOJ1430 小猴打架
【费马小定理】BZOJ3260 跳
【欧拉函数】BZOJ2705: [SDOI2012]Longge的问题
【卡特兰数】BZOJ1485: [HNOI2009]有趣的数列
【缩点+拓扑判链】POJ2762 Going from u to v or from v to u?
【Floyd】BZOJ1491: [NOI2007]社交网络
【转】对信息学竞赛中调试方法的建议
【建图+拓扑判环】BZOJ3953: [WF2013]Self-Assembly
【dfs判负环】BZOJ1489: [HNOI2009]最小圈
【二分+最小树形图】UVA11865 比赛网络
-
原文地址:https://www.cnblogs.com/abcdwxc/p/972278.html
Copyright © 2020-2023
润新知