当前位置:网站首页>Direct insert sort and shell sort
Direct insert sort and shell sort
2022-06-11 01:13:00 【Ping_ qing】
List of articles
1、 Direct insert sort
1.1、 Basic introduction and thought
Insertion sort belongs to internal sort method , It is to find the appropriate position of the pre sorted element by inserting , In order to achieve the purpose of sorting .
Insertion sort (Insertion Sorting) The basic idea of : hold n The elements to be sorted look like an ordered table and an unordered table , The ordered table starts with just one element , The unordered table contains n-1 Elements , The first element is taken out of the unordered table every time during sorting , Compare its sort code with the sort code of the ordered table elements in turn , Insert it in place in an ordered table , Make it a new ordered table .
1.2、 Code implementation
// Ascending sort
public class InsertSort {
public static void main(String[] args) {
int[] arr = {
9,4,2,6,3,7,4,1};
insertSort(arr);
System.out.println(Arrays.toString(arr));
}
// Insertion sort
public static void insertSort(int[] arr) {
int insertVal = 0;
int insertIndex = 0;
for (int i = 1; i < arr.length; i++) {
// Define the number to be inserted
insertVal = arr[i];
insertIndex = i - 1; // namely arr[i] The subscript of the preceding number of
// to insertVal Find where to insert
// explain
//1.insertIndex >=0 Promise to give insertVal Find insertion location , Don't cross the border
//2.insertVal < arr[insertIndex] The number to be inserted , We haven't found the insertion position yet
//3. You need to arr[insertIndex] Move backward
while (insertIndex >= 0 && insertVal < arr[insertIndex]) {
// If sorting from large to small , Change the less than sign to the greater than sign
arr[insertIndex + 1] = arr[insertIndex];
insertIndex--;
}
// When to exit while loop , Indicate where to insert found , namely insertIndex+1
int curIndex = insertIndex + 1;
if (insertIndex != i) {
arr[curIndex] = insertVal;
}
}
}
}
2、 Shell Sort
2.1、 Problems with simple insertion sort
Possible problems with simple insertion sorting .
Array arr = {2,3,4,5,6,1} The number to be inserted at this time 1( Minimum ), It's a process like this :
{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}
Conclusion : When the number to be inserted is a smaller number , The number of moves back has increased significantly , It has an impact on efficiency .
2.2、 Introduction and basic idea of Hill's sorting method
The Hill order is Hill (Donald Shell) On 1959 A sort algorithm proposed in . Hill sort is also an insert sort , It's a more efficient version of simple insert sorting with improvements , Also known as Reduced delta sort .
Hill sort The basic idea : Hill sort is to group records by a certain increment of subscript , Sort each group using the direct insertion sort algorithm ; As the increments decrease , Each group contains more and more keywords , When the increment is reduced to 1 when , The whole document is just a group , The algorithm terminates
Hill sort diagram

- Finally, insert and sort all the data .
2.3、 Exchange method code
// Insertion sort - Shell Sort
public class ShellSort {
public static void main(String[] args) {
int[] arr = {
8, 9, 1, 7, 2, 3, 5, 4, 6, 0};
shellSort(arr);
}
// Shell Sort - Exchange method ( Low efficiency )
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++) {
// Go through all the elements in each group ( common gap Group , Each group has length/gap Elements ), In steps of gap
for (int j = i - gap; j >= 0; j -= gap) {
// If the current uniform speed is greater than the element after adding the step size , Explain the exchange
if (arr[j] > arr[j + gap]) {
temp = arr[j];
arr[j] = arr[j + gap];
arr[j + gap] = temp;
}
}
}
System.out.println(" Hill ranked second "+(++count)+" round ="+ Arrays.toString(arr));
}
}
}
2.4、 Moving method code ( recommend )
package com.ping.sort;
import java.util.Arrays;
// Insertion sort - Shell Sort
public class ShellSort {
public static void main(String[] args) {
int[] arr = {
8, 9, 1, 7, 2, 3, 5, 4, 6, 0};
shellSort2(arr);
System.out.println(" After ordering "+Arrays.toString(arr));
}
// Optimize the Hill sort of exchange -> Shift method
public static void shellSort2(int[] arr) {
int count = 0;
// The incremental gap, And gradually reduce the increment
for (int gap = arr.length / 2; gap > 0; gap /= 2) {
// From gap Elements , Insert and sort the groups one by one
for (int i = gap; i < arr.length; i++) {
int j = i;
int temp = arr[j];
// This if Just insert the sort directly before ,while The last one inside if Well , Just change positions to improve efficiency
if(arr[j] <arr[j-gap]){
while(j - gap >= 0 && temp <arr[j-gap]){
// Move
arr[j] = arr[j-gap];
j -= gap;
}
// When to exit while after , Just give it to temp Find where to insert
arr[j] = temp;
}
}
}
}
}
边栏推荐
- STM32 cannot download again after downloading the code
- CentOS actual deployment redis
- 库存管理与策略模式
- Alicloud configures SLB (load balancing) instances
- Store binary tree in sequence [store tree in array]
- 循环结构语句
- zabbix离线安装
- 最好的創意鼓工具:Groove Agent 5
- [paper translation] recent advances in open set recognition: a survey
- WPF - timeline class
猜你喜欢

ZABBIX offline installation

cosine 相似度计算总结
![[论文阅读] FixMatch: Simplifying Semi-Supervised Learning with Consistency and Confidence](/img/86/72726f933deef6944b62149759b7d5.png)
[论文阅读] FixMatch: Simplifying Semi-Supervised Learning with Consistency and Confidence

【ROS入门教程】---- 03 单片机、PC主机、ROS通信机制

87. (leaflet house) leaflet military plotting - straight arrow modification

Adapter mode

MySQL
![[paper reading] boostmis: boosting medical image semi supervised learning with adaptive pseudolabeling](/img/10/a60cfe830e2238de00121d7bd95ad6.png)
[paper reading] boostmis: boosting medical image semi supervised learning with adaptive pseudolabeling
WSL 自动更新 ip hosts 文件

About the log traffic monitoring and early warning small project | standardized return of interaction with the database in flask
随机推荐
Hash table (hash table \u hashtable)_ Array + linked list_ Code for employee management
最好的创意鼓工具:Groove Agent 5
compiler explorer
网络基础(1)-----认识网络
ion_dma_buf_begin_cpu_access
Alicloud configures SLB (load balancing) instances
Teach you the front and back separation architecture (V) system authentication implementation
zabbix离线安装
pytorch分类问题总结
[论文阅读] BoostMIS: Boosting Medical Image Semi-supervised Learning with Adaptive Pseudo Labeling
Why web development with golang
WPF - timeline class
北京中国专利奖政策支持介绍,补贴100万
为什么使用 Golang 进行 Web 开发
用data和proc怎么写出这个,不用sql
深圳中国专利奖申报流程介绍,补贴100万
Josephus problem_ Unidirectional circular linked list_ code implementation
day01
One way linked list to realize student information management
配置化自定义实现1.实现接口,2.自定义配置3.默认配置