希尔排序

package 排序;

/**
 * 
 * @author 赵鹏
 * @date 2013年12月20日
 * 希尔排序
 * 
 * 百科解释:
 * 先取一个小于n的整数d1作为第一个增量,把文件的全部记录分成d1个组。
 * 所有距离为dl的倍数的记录放在同一个组中。先在各组内进行直接插人排序;
 * 然后,取第二个增量d2<d1重复上述的分组和排序,
 * 直至所取的增量dt=1(dt<dt-l<…<d2<d1),
 * 即所有记录放在同一组中进行直接插入排序为止。
 * 
 * 大话数据结构文章
 * @see http://www.cnblogs.com/cj723/archive/2011/04/19/2021613.html
 * @see http://www.cnblogs.com/cj723/archive/2011/04/20/2021648.html
 *
 */
public class ShellSort
{
	
	/**
	 * 每一组内进行插入排序,增量为1之后基本顺序形成再进行直接插入排序
	 * @param array
	 */
	public static void sort(int[] array)
	{
		int increment=array.length;
		int i,j,temp;
		while((increment=increment/3+1)>1)
		{
			for(i=increment;i<array.length;i++)
			{
				temp=array[i];
				if(temp<array[i-increment])
				{
					for(j=i-increment;j>=0&&array[j]>temp;j-=increment)
					{
						array[j+increment]=array[j];
					}
					array[j+increment]=temp;
				}
			}
		}
		directInsertSort(array);
	}

	public static void directInsertSort(int[] array)
	{
		int i,j,temp;
		for(i=1;i<array.length;i++)
		{
			temp=array[i];
			j=i-1;
			while(j>=0&&array[j]>temp)
			{
				array[j+1]=array[j];
				j--;
			}
			array[j+1]=temp;
		}
	}
	
	public static void test(int[] testarray,int testcount)
	{
		long sum=0,temp;
		for(int i=1;i<=testcount;i++)
		{
			int[] t=testarray.clone();
			long begin1=System.currentTimeMillis();
			sort(t);
			long end1=System.currentTimeMillis();
			//System.out.println(Arrays.toString(a));
			temp=end1-begin1;
			if(i>5) sum+=temp;
			//System.out.println("time"+i+":"+temp);
		}
		System.out.println("ShellSort.sort()\taveg:"+(sum/(testcount-5)));
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值