您的位置:首页 > 运维架构 > Shell

常见排序算法:shell排序

2010-03-25 10:53 127 查看
shell排序是对插入排序的一个改装,它每次排序把序列的元素按照某个增量分成几个子序列,对这几个子序列进行插入排序,然后不断的缩小增量扩大每个子序列的元素数量,直到增量为一的时候子序列就和原先的待排列序列一样了,此时只需要做少量的比较和移动就可以完成对序列的排序了。

1 /// <summary>
2 /// default to sort assending
3 /// execute the shell sorting;
4 /// </summary>
5 /// <param name="array"></param>
6 public static void SortByAssending(int[] array)
7 {
8 // check argument;
9
int tmp = 0;
int j = 0;
int i = 0;
for( int increment = array.Length/2;increment >0; increment/=2)
{
for(i = increment ; i< array.Length; i++)
{
tmp = array[i];
for(j = i;j>=increment;j-=increment)
{
if(array[j-increment] > tmp)
{
array[j] = array[j-increment];
}
else
{
break;
}
}
array[j] = tmp;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: