当前位置:网站首页>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 framework to study record 6 - the torch. Nn. The Module and the torch nn. Functional. The use of conv2d
- 权值线段树+线段树分裂/合并+CF1659D
- Reverse Analysis Practice 2
- Pytorch框架学习记录3——Transform的使用
- New LaaS protocol Elephant Swap provides ePLATO with sustainable premium space
- error: The following untracked working tree files would be overwritten by
- Mysql version upgrade, copy the Data file directly, the query is very slow
- Flink学习第一天——什么是批量、流式计算?
- Taobao/Tmall get Taobao store details API
- Operational configuration: How to run multiple EasyCVR programs as a service in one server?
猜你喜欢

What are Redis server startup after the operation?

Reverse Analysis Practice 2

Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Products (3) Background Functions

Android Studio 实现登录注册-源代码 (连接MySql数据库)

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

swagger使用教程——快速使用swagger

ospf map

Pytorch framework learning record 7 - convolutional layer

Many overseas authoritative media hotly discuss TRON: laying the foundation for the decentralization of the Internet

Charles replaces the interface response information
随机推荐
Mini Program Graduation Works WeChat Second-hand Trading Mini Program Graduation Design Finished Works (4) Opening Report
Mysql version upgrade, copy the Data file directly, the query is very slow
RRU、BBU、AAU
弘玑再度入围Gartner 2022 RPA魔力象限并实现位置大幅跃升
Pytorch框架学习记录2——TensorBoard的使用
JQ源码分析(环境处理)
What are Redis server startup after the operation?
The underlying mechanism of the function
Wechat second-hand transaction small program graduation design finished product (1) Development overview
Anti-shake and throttling
SQLSERVER merges subquery data into one field
逆向分析实战2
海外多家权威媒体热议波场TRON:为互联网去中心化奠定基础
获取本机IP和Request的IP
FreeRTOS Personal Notes - Memory Management
Thymeleaf简介
(6) "Digital Electricity" - Diodes and CMOS Gate Circuits (Introduction)
使用EFR32作为Zigbee/Thread的sniffer的用法
ospf 综合实验(重发布,特殊区域)
Redis【超详解!!!】