插入排序之希尔排序(Java)
博客说明
文章所涉及的资料来自互联网整理和个人总结,意在于个人学习和经验汇总,如有什么地方侵权,请联系本人删除,谢谢!
希尔排序法介绍
希尔排序是希尔(Donald Shell)于 1959 年提出的一种排序算法。希尔排序也是一种插入排序,它是简单插入排序经过改进之后的一个更高效的版本,也称为缩小增量排序。
希尔排序法基本思想
希尔排序是把记录按下标的一定增量分组,对每组使用直接插入排序算法排序;随着增量逐渐减少,每组包含的关键词越来越多,当增量减至 1 时,整个文件恰被分成一组,算法便终止
代码(交换式)
package cn.guizimo.sort;
import java.util.Arrays;
public class ShellSort {public static void main(String[] args) {int[] arr = {8, 9, 1, 7, 2, 3, 5, 4, 6, 0};
System.out.println("排序前");
System.out.println(Arrays.toString(arr));
shellSort(arr);
System.out.println("排序后");
System.out.println(Arrays.toString(arr));
}
public static void shellSort(int[] arr) {
int count = 0;
for (int gap = arr.length / 2; gap > 0; gap /= 2) {
count++;
int temp = 0;
for (int i = gap; i < arr.length; i++) {for (int j = i - gap; j >= 0; j -= gap) {if (arr[j] > arr[j + gap]) {temp = arr[j];
arr[j] = arr[j + gap];
arr[j + gap] = temp;
}
}
}
System.out.println("第"+count+"轮排序");
System.out.println(Arrays.toString(arr));
}
}
}
测试
测试速度
package cn.guizimo.sort;
import java.util.Arrays;
public class ShellSort {public static void main(String[] args) {
int max = 80000;
int[] arr = new int[max];
for (int i = 0; i < max; i++) {arr[i] = (int)(Math.random() * 8000000);
}
long date1 = System.currentTimeMillis();
shellSort(arr);
long date2 = System.currentTimeMillis();
System.out.println("交换式希尔排序"+max+"数组的时间为:"+(date2-date1));
}
public static void shellSort(int[] arr) {for (int gap = arr.length / 2; gap > 0; gap /= 2) {
int temp = 0;
for (int i = gap; i < arr.length; i++) {for (int j = i - gap; j >= 0; j -= gap) {if (arr[j] > arr[j + gap]) {temp = arr[j];
arr[j] = arr[j + gap];
arr[j + gap] = temp;
}
}
}
}
}
}
代码(位移式)
package cn.guizimo.sort;
import java.util.Arrays;
public class ShellSort {public static void main(String[] args) {int[] arr = {8, 9, 1, 7, 2, 3, 5, 4, 6, 0};
System.out.println("排序前");
System.out.println(Arrays.toString(arr));
shellSort(arr);
System.out.println("排序后");
System.out.println(Arrays.toString(arr));
}
public static void shellSort(int[] arr) {
int count = 0;
for (int gap = arr.length / 2; gap > 0; gap /= 2) {
count++;
for (int i = gap; i < arr.length; i++) {
int j = i;
int temp = arr[i];
if(arr[j]<arr[j-gap]){while (j-gap >= 0 && temp < arr[j-gap]){arr[j] = arr[j-gap];
j -= gap;
}
arr[j] = temp;
}
}
System.out.println("第"+count+"轮排序");
System.out.println(Arrays.toString(arr));
}
}
}
测试
速度测试
package cn.guizimo.sort;
import java.util.Arrays;
public class ShellSort {public static void main(String[] args) {
int max = 80000;
int[] arr = new int[max];
for (int i = 0; i < max; i++) {arr[i] = (int)(Math.random() * 8000000);
}
long date1 = System.currentTimeMillis();
shellSort(arr);
long date2 = System.currentTimeMillis();
System.out.println("位移式希尔排序"+max+"数组的时间为:"+(date2-date1));
}
public static void shellSort(int[] arr) {for (int gap = arr.length / 2; gap > 0; gap /= 2) {for (int i = gap; i < arr.length; i++) {
int j = i;
int temp = arr[i];
if(arr[j]<arr[j-gap]){while (j-gap >= 0 && temp < arr[j-gap]){arr[j] = arr[j-gap];
j -= gap;
}
arr[j] = temp;
}
}
}
}
}
感谢
尚硅谷
万能的网络
以及勤劳的自己