当前位置:网站首页>2.4希尔排序
2.4希尔排序
2022-07-30 04:16:00 【TUJC】
2.4、希尔排序
2.4.1、简单插入排序存在的问题
我们看简单的插入排序可能存在的问题.
数组arr = {2,3,4,5,6,1}这时需要插入的数1(最小),这样的过程是:
{
2,3,4,5,6,6}
{
2,3,4,5,5,6}
{
2,3,4,4,5,6}
{
2,3,3,4,5,6}
{
2.2,3,4,5,6}
{
1,2,3,4,5,6}
结论:当需要插入的数是较小的数时,后移的次数明显增多,对效率有影响.
2.4.2、希尔排序法介绍
希尔排序是希尔(Donald Shell))于1959年提出的一种排序算法。希尔排序也是一种插入排序,它是简单插入排序经过改进之后的一个更高效的版本,也称为缩小增量排序。
希尔排序法基本思想:
希尔排序是把记录按下标的一定增量分组,对每组使用直接插入排序算法排序,随着增量逐渐减少,每组包含的关键词越来越多,当增量减至1时,整个文件恰被分成一组,算法便终止。
希尔排序法的示意图:


2.4.2、代码实现
考试成绩分别是{8,9,1,7,2,3,5,4,6,0}请从小到大排序.请分别使用
- 1)希尔排序时,对有序序列在插入时采用交换法,并测试排序速度.
- 2)希尔排序时,对有序序列在插入时采用移动法,并测试排序速度
package com.atguigu.sort;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
public class ShellSort {
public static void main(String[] args) {
//int[] arr = { 8, 9, 1, 7, 2, 3, 5, 4, 6, 0 };
// 创建要给80000个的随机的数组
int[] arr = new int[8000000];
for (int i = 0; i < 8000000; i++) {
arr[i] = (int) (Math.random() * 8000000); // 生成一个[0, 8000000) 数
}
System.out.println("排序前");
Date data1 = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date1Str = simpleDateFormat.format(data1);
System.out.println("排序前的时间是=" + date1Str);
//shellSort(arr); //交换式
shellSort2(arr);//移位方式
Date data2 = new Date();
String date2Str = simpleDateFormat.format(data2);
System.out.println("排序前的时间是=" + date2Str);
//System.out.println(Arrays.toString(arr));
}
// 使用逐步推导的方式来编写希尔排序
// 希尔排序时, 对有序序列在插入时采用交换法,
// 思路(算法) ===> 代码
public static void shellSort(int[] arr) {
int temp = 0;
int count = 0;
// 根据前面的逐步分析,使用循环处理
for (int gap = arr.length / 2; gap > 0; gap /= 2) {
for (int i = gap; i < arr.length; i++) {
// 遍历各组中所有的元素(共gap组,每组有个元素), 步长gap
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) + "轮 =" + Arrays.toString(arr));
}
/* // 希尔排序的第1轮排序 // 因为第1轮排序,是将10个数据分成了 5组 for (int i = 5; i < arr.length; i++) { // 遍历各组中所有的元素(共5组,每组有2个元素), 步长5 for (int j = i - 5; j >= 0; j -= 5) { // 如果当前元素大于加上步长后的那个元素,说明交换 if (arr[j] > arr[j + 5]) { temp = arr[j]; arr[j] = arr[j + 5]; arr[j + 5] = temp; } } } System.out.println("希尔排序1轮后=" + Arrays.toString(arr));// // 希尔排序的第2轮排序 // 因为第2轮排序,是将10个数据分成了 5/2 = 2组 for (int i = 2; i < arr.length; i++) { // 遍历各组中所有的元素(共5组,每组有2个元素), 步长5 for (int j = i - 2; j >= 0; j -= 2) { // 如果当前元素大于加上步长后的那个元素,说明交换 if (arr[j] > arr[j + 2]) { temp = arr[j]; arr[j] = arr[j + 2]; arr[j + 2] = temp; } } } System.out.println("希尔排序2轮后=" + Arrays.toString(arr));// // 希尔排序的第3轮排序 // 因为第3轮排序,是将10个数据分成了 2/2 = 1组 for (int i = 1; i < arr.length; i++) { // 遍历各组中所有的元素(共5组,每组有2个元素), 步长5 for (int j = i - 1; j >= 0; j -= 1) { // 如果当前元素大于加上步长后的那个元素,说明交换 if (arr[j] > arr[j + 1]) { temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } System.out.println("希尔排序3轮后=" + Arrays.toString(arr));// */
}
//对交换式的希尔排序进行优化->移位法
public static void shellSort2(int[] arr) {
// 增量gap, 并逐步的缩小增量
for (int gap = arr.length / 2; gap > 0; gap /= 2) {
// 从第gap个元素,逐个对其所在的组进行直接插入排序
for (int i = gap; i < arr.length; i++) {
int j = i;
int temp = arr[j];
if (arr[j] < arr[j - gap]) {
while (j - gap >= 0 && temp < arr[j - gap]) {
//移动
arr[j] = arr[j-gap];
j -= gap;
}
//当退出while后,就给temp找到插入的位置
arr[j] = temp;
}
}
}
}
}
边栏推荐
- Pytorch框架学习记录2——TensorBoard的使用
- swagger使用教程——快速使用swagger
- Taobao/Tmall get the list of sold product orders API
- Reverse Theory Knowledge 3 [UI Modification]
- Why is the Kirin 9000 5G version suddenly back in stock?
- STM32 SPI+WM8978 voice loopback
- ospf map
- (redistribute, special comprehensive experiment ospf area)
- Pytorch框架学习记录4——数据集的使用(torchvision.dataset)
- 2021山东省网络搭建与应用赛项试题
猜你喜欢

在麒麟V10操作系统上安装MySQL数据库

Redis server启动后会做哪些操作?

WEB 渗透之信息收集

Eureka Registry

Pytorch框架学习记录4——数据集的使用(torchvision.dataset)

Pytorch framework learning record 4 - the use of datasets (torchvision.dataset)

cnpm安装步骤

Mini Program Graduation Works WeChat Second-hand Trading Mini Program Graduation Design Finished Works (3) Background Functions

Forum management system
![[Switch] Protocol-Oriented Programming in Swift: Introduction](/img/7a/6db558231a77ad9e739b571cf328d6.png)
[Switch] Protocol-Oriented Programming in Swift: Introduction
随机推荐
Unity3D Application模拟进入前后台及暂停
Pytorch framework learning record 7 - convolutional layer
High Concurrency Framework Disruptor
商品管理系统数据库设计--SQL Server
Azure 开发者新闻快讯丨开发者7月大事记一览
MySql 怎么查出符合条件的最新的数据行?
【转】Swift 中的面向协议编程:引言
数据目录是什么?为何需要它?
Boutique: Taobao/Tmall Get Order Details API for Purchased Products
为什么突然间麒麟 9000 5G 版本,又有库存了?
Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Work (7) Interim Inspection Report
QT(39)-vs开发qt程序提示无法打开源文件
cnpm安装步骤
Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Products (1) Development Overview
How does the AI intelligent security video platform EasyCVR configure the simultaneous transmission of audio and video?
state space representation
How to compare struct, slice, map for equality and the difference between several comparison methods in golang
Solve the problem of compiling and installing gdb-10.1 unistd.h:663:3: error: #error “Please include config.h first.”
ospf map
Pytorch framework to study record 6 - the torch. Nn. The Module and the torch nn. Functional. The use of conv2d