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)));
}
}
希尔排序
最新推荐文章于 2024-09-12 19:02:20 发布