当前位置:网站首页>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;
}
}
}
}
}
边栏推荐
- 获取本机IP和Request的IP
- C. Qualification Rounds(思维,特情)
- (redistribute, special comprehensive experiment ospf area)
- STM32 SPI+WM8978 voice loopback
- Flink学习第一天——什么是批量、流式计算?
- After 5 years of Ali internship interview~
- Many overseas authoritative media hotly discuss TRON: laying the foundation for the decentralization of the Internet
- How to solve the error "no such file or directory" when EasyCVR starts?
- MySQL data query (subtotal and sorting)
- Detailed transport layer
猜你喜欢

WEB penetration of information collection

图像视角矫正之透视变换矩阵(单应矩阵)/findHomography 与 getPerspectiveTransformd的区别

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

Pytorch框架学习记录2——TensorBoard的使用

Eureka Registry

ospf 综合实验(重发布,特殊区域)

Shell脚本基本编辑规范及变量

MySQL 安装报错的解决方法

What are Redis server startup after the operation?

Pytorch框架学习记录3——Transform的使用
随机推荐
网页元素解析a标签
How to Effectively Conduct Retrospective Meetings (Part 1)?
【翻译】Envoy Fundamentals,这是一个培训课程,使人们能够更快地采用Envoy Proxy。...
The first immersive and high-fidelity metaverse in China, Xiyuan Universe is officially launched
为什么突然间麒麟 9000 5G 版本,又有库存了?
MySQL 操作语句大全(详细)
Charles 替换 接口响应信息
Mini Program Graduation Works WeChat Second-hand Trading Mini Program Graduation Design Finished Works (3) Background Functions
OA Project Pending Meeting & History Meeting & All Meetings
Based on all volunteers - H and D1 XR806 rare plant monitoring device
CMake installation and testing
Reverse Analysis Practice 2
sublime text 3 settings
新型LaaS协议Elephant Swap给ePLATO提供可持续溢价空间
Flink学习第一天——什么是批量、流式计算?
Pytorch framework learning record 2 - the use of TensorBoard
海外多家权威媒体热议波场TRON:为互联网去中心化奠定基础
The first day of Flink learning - what is batch and streaming computing?
Drools (7): WorkBench
弘玑再度入围Gartner 2022 RPA魔力象限并实现位置大幅跃升