当前位置:网站首页>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;
}
}
}
}
}
边栏推荐
- 骁龙7系芯片表现如何?Reno8 Pro佐证新一代神U
- Taobao/Tmall get Taobao store details API
- Redis【超详解!!!】
- Wechat second-hand transaction small program graduation design finished product (1) Development overview
- Data Lake: Data Integration Tool DataX
- 【Untitled】
- 逆向理论知识3【UI修改篇】
- The implementation and basic operation of sub-database sub-table, ER table, global table, fragmentation rules, global sequence, etc. in MyCat
- 【驱动】udev为USB转4串口的每个串口起别名
- Mini Program Graduation Works WeChat Second-hand Trading Mini Program Graduation Design Finished Works (3) Background Functions
猜你喜欢

The leap second that may cause the next "Millennium Bug" is boycotted by tech giants

Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Products (1) Development Overview

ospf map

逆向分析实战2

【C进阶】数组传参与函数指针

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

The implementation and basic operation of sub-database sub-table, ER table, global table, fragmentation rules, global sequence, etc. in MyCat

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

使用EFR32作为Zigbee/Thread的sniffer的用法

How to solve the error "no such file or directory" when EasyCVR starts?
随机推荐
sublime text 3 settings
Pytorch framework learning record 5 - the use of DataLoader
Pytorch框架学习记录1——Dataset类代码实战
权值线段树+线段树分裂/合并+CF1659D
代码开源设计实现思路
Unity3D Application模拟进入前后台及暂停
The difference between forward and redirect
Eureka注册中心
Is the end of the universe a bank?Talk about those things about doing software testing in the bank
MySQL 字符串拼接 - 多种字符串拼接实战案例
[ 云原生之谜 ] 云原生背景 && 定义 && 相关技术详解?
基于OpenCV实现的图像拼接(配准)案例
After 5 years of Ali internship interview~
The difference between BGP room and ordinary room in Beijing
【翻译】Envoy Fundamentals,这是一个培训课程,使人们能够更快地采用Envoy Proxy。...
Has been empty, a straightforward, continue to copy the top off!
MySQL 操作语句大全(详细)
网页元素解析a标签
函数的底层机制
JQ源码分析(环境处理)