首页 > 编程语言 > 详细

排序算法总结之希尔排序

时间:2016-02-16 18:40:32      阅读:131      评论:0      收藏:0      [点我收藏+]
package com.util.sort;

public class ShellSort {

	private long[]theArray;
	private int nElem;
	public ShellSort(int max){
		theArray = new long[max];
		nElem = 0;
	}
	//插入元素
	public void insert(long value){
		theArray[nElem] = value;
		nElem++;
	}
	//打印元素
	public void display(){
		for (int i = 0;i<theArray.length;i++) {
			System.out.print(theArray[i]+" ");
		}
		System.out.println();
	}
	
	public void sort(){
		System.out.println("MMMMMMMMMMM");
		int inner = 0;
		int outer = 0;
		long temp = 0;
		int h = 1;
		while (h <= nElem/3) 
			h = h*3+1;
			while(h > 0){
				for (outer = h;outer<nElem;outer++) {
					temp = theArray[outer];
					inner = outer;
					while (inner > h-1 && theArray[inner-h] >= temp) {
						theArray[inner] = theArray[inner-h];
						inner -= h;
						
					}
					theArray[inner] = temp;
					
				}
				h = (h-1)/3;
			}
		
	}
	
	
}

 

排序算法总结之希尔排序

原文:http://www.cnblogs.com/airycode/p/5193124.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!