排序算法之希尔排序

希尔排序

将数据分成m列,逐列进行排序(采用插入排序)。
先分成8列进行排序,然后是4,2,1
先分成8列进行排序,然后是4,2,1
下面展示一些 内联代码片

package com.sortAlgorithm;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class ShellSort<E extends Comparable<E>> extends Sort<E> {
	
	@Override
	protected void sort() {
		List<Integer> stepSequence = shellStepSequence();
		//List<Integer> stepSequence = sedgewickStepSequence();
		for (Integer step : stepSequence) {
			sort(step);
		}
	}
	
	/**
	 * 分成 step 列进行排序
	 * @param step
	 */
	private void sort(int step) {
		//col:第几列, column的简称
		
		for (int col = 0; col < array.length; col++) {//对第col进行排序
			
			//col、col+step、col+2*step、col+3*step、
			for(int begin = col + step; begin < array.length; begin += step) {
				int cur = begin;
				while(cur > col && cmp(cur, cur - step) < 0) {
					swap(cur, cur - step);
					cur -= step;
				}
			}
		}
	}

	//希尔本人给出的步长序列,最坏情况时间复杂度是O(n^2)
	private List<Integer> shellStepSequence(){
		List<Integer> stepSequence = new ArrayList<>();
		int step = array.length;
		
		while ((step >>= 1) > 0) {
			stepSequence.add(step);
		}
		
		return stepSequence;
	}
	
	//Robert Sedgewick 给出的步长序列最坏情况时间复杂度是O(n^4/3),是目前已知的最好的步长序列
	private List<Integer> sedgewickStepSequence(){
		List<Integer> stepSequence = new LinkedList<>();
		int k = 0, step = 0;
		while (true) {
			if (k % 2 == 0) {
				int pow = (int) Math.pow(2, k >> 1);
				step = 1 + 9 * (pow * pow - pow);
			}else {
				int pow1 = (int) Math.pow(2, (k - 1) >> 1);
				int pow2 = (int) Math.pow(2, (k + 1) >>1);
				step = 1 + 8 * pow1 * pow2 - 6 * pow2;
			}
			if(step >= array.length) break;
			stepSequence.add(0,step);
			k++;
		}
		return stepSequence;
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值