`

java算法之希尔排序

 
阅读更多
package com.hym.test.algorithms;

public class ShellSort {
	private int[] arrayTest = { 5, 26, 1, 783, 23, 2, 62, 9, 46 };

	public void shellSort() {
		int begin, end;
		int temp;

		int h = 1;
		while (h <= arrayTest.length / 3) {
			h = 3 * h + 1;
		}

		while (h > 0) {
			for (end = h; end < arrayTest.length; end++) {
				temp = arrayTest[end];
				begin = end;

				while (begin > h - 1 && arrayTest[begin - h] >= temp) {
					arrayTest[begin] = arrayTest[begin - h];
					begin -= h;
				}
				arrayTest[begin] = temp;
			}
			h = (h - 1) / 3;
		}
	}

	public static void main(String[] args) {
		ShellSort sort = new ShellSort();
		sort.shellSort();

		for (int i = 0; i < sort.arrayTest.length; i++) {
			System.out.print(sort.arrayTest[i] + " ");
		}
	}
}



希尔排序(Shell Sort)是插入排序的一种。是针对直接插入排序算法的改进。该方法又称缩小增量排序,因DL.Shell于1959年提出而得名。

http://baike.baidu.com/view/178698.htm
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics