排序算法一 || 希尔排序
基本思想:
希尔排序是把序列按下标的一定增量分组,对每组使用直接插入排序算法排序;随着增量的逐渐减少,每组包含的关键词越来越多,当增量减至1时,整个序列恰好被分为一组,算法便终止。
图文示例:
步骤1:
步骤2:
步骤3:
源码实现:
//希尔排序
public class ShellSort {
public static void shell(int[] args){
int length = args.length;
for (int gap = length/2;gap > 0;gap = gap/2){
for (int i = gap;i < length;i++)
{
int j = i;
while (j - gap >= 0 && args[j] < args[j - gap])
{
int temp = args[j];
args[j] = args[j-gap];
args[j-gap] = temp;
j = j - gap;
}
}
}
}
public static void main(String[] args) {
int[] arg = {65,32,4,212,98,23,53,55,32,2,324,235,5,342,145,25,5,7,473,252,545,55,37,579,923,60};
shell(arg);
for (int i = 0; i < arg.length; i++) {
System.out.print(arg[i]+" ");
}
}
}