希尔排序之java实现

package com.cb.java.algorithms.datastructure.yanweimindatastructure.sort;


/**
 * 希尔排序
 * 
 * @author 36184
 *
 */
public class ShellSort<T extends Comparable<T>> {


/**
* 希尔排序
* @param arr
*/
public void shellSort(T[] arr) {
int h = 0;
while (h * 3 + 1 < arr.length) {
h=h*3+1;
}
while(h>0)
{
for (int i = 0; i < arr.length; i += h) {

for(int j=i+h;j>0;j-=h)
{
if(j<arr.length)
{
T temp=arr[j];
if(temp.compareTo(arr[j-h])<0)
{
arr[j]=arr[j-h];
arr[j-h]=temp;
}
}
}
}
h=(h-1)/3;
}

}


public static void main(String[] args) {
ShellSort<Integer>bubb=new ShellSort<>();
Integer[]arr=new Integer[]{3,2,10,5,35,27,1000,453,34,57,89};
bubb.shellSort(arr);
for(Integer i:arr)
{
System.out.print(i+" ");
}
}
}

猜你喜欢

转载自blog.csdn.net/u013230189/article/details/80677953