当前位置:网站首页>Hill sort c language
Hill sort c language
2022-07-06 07:59:00 【Tengban monster】
Read it in pieces
Hill sort is a kind of insertion sort , It's an improved version of direct insert sort .
Let's first look at direct insertion sorting , The basic idea is , Usually isolate the first number of this pile of numbers first , Then one of its own is order , Then compare the latter number with it , Find the right size and position to insert , After that, the small pile is still orderly , Then compare the back one with the front one , Find the right place to insert , Until it's all plugged in .
Advantages of direct insertion sorting
From the idea of direct insertion sorting, we can know , If this pile of numbers had been more orderly , So direct insertion sorting is very efficient , Because the number of exchanges will be much less .
Disadvantages of direct insertion sorting
But one situation is , Suppose it is sorted from small to large , Ten thousand ordinal numbers have been arranged in front , Here we are 10001 When we count , It happens to be the smallest one , Then you have to exchange all the previous numbers ,, This leads to the problem that the exchange distance is too long , This is what we don't want to see .
Shell Sort
Based on these two characteristics of direct insertion sorting , We introduced an upgraded version of it —— Shell Sort .
Hill sort is also called reduced incremental sort .
In order to solve the problem that the exchange distance of direct insertion sorting is too long , We imagine that if we can put the smaller number in this pile of numbers to be sorted aside , The larger number is on the other side , Then you don't have to cross mountains and rivers when the exchange occurs .
in other words , We need to pre sort this pile of numbers first , The specific operation is , Select a number first interval( Usually half of the total number of numbers ) As the first increment , Then set the interval to interval As a group , Sort this group by direct insertion , Then shrink interval( In general, let's go interval halve ) As the second increment , Continue to sort by direct insertion in groups , And so on , Go on , To interval be equal to 1 When , The interval between groups is 1, That is to insert and sort all elements directly , But all the time , This pile of numbers has been relatively orderly , Direct insertion sorting is very efficient at this time .
Code
#include<stdio.h>
void insert(int a[],int n)
{
int i,temp,j,interval;
while(interval>0)// Compared with direct insertion , Namely i and j Changes in interval In units .
{
for(i=interval;i<n;i=i+interval)// Divide into groups .
for(j=i;j>0;j=j-interval)// Direct insertion sorting in groups .
if(a[j]<a[j-interval])
{
temp=a[j];
a[j]=a[j-interval];
a[j-interval]=temp;
}
interval=interval/2;
}
}
int main()
{
int a[10]={7,3,1,6,2,0,5,8,4,9};
insert(a,10);
for(int i=0;i<10;i++)
printf("%d ",a[i]);
}
link —— Direct insert sort
https://blog.csdn.net/weixin_62264287/article/details/122895216?spm=1001.2014.3001.5501
边栏推荐
- Data governance: metadata management
- "Designer universe" APEC design +: the list of winners of the Paris Design Award in France was recently announced. The winners of "Changsha world center Damei mansion" were awarded by the national eco
- Position() function in XPath uses
- edge瀏覽器 路徑獲得
- "Friendship and righteousness" of the center for national economy and information technology: China's friendship wine - the "unparalleled loyalty and righteousness" of the solidarity group released th
- Database basic commands
- wincc7.5下载安装教程(Win10系统)
- P3047 [USACO12FEB]Nearby Cows G(树形dp)
- C # connect to SQLite database to read content
- Epoll and IO multiplexing of redis
猜你喜欢
Interview Reply of Zhuhai Jinshan
"Designer universe": "benefit dimension" APEC public welfare + 2022 the latest slogan and the new platform will be launched soon | Asia Pacific Financial Media
Qualitative risk analysis of Oracle project management system
[count] [combined number] value series
23. Update data
Easy to use tcp-udp_ Debug tool download and use
Nft智能合约发行,盲盒,公开发售技术实战--合约篇
Machine learning - decision tree
【T31ZL智能视频应用处理器资料】
在 uniapp 中使用阿里图标
随机推荐
Launch APS system to break the problem of decoupling material procurement plan from production practice
1015 reversible primes (20 points) prime d-ary
Luogu p4127 [ahoi2009] similar distribution problem solution
继电反馈PID控制器参数自整定
MFC 给列表控件发送左键单击、双击、以及右键单击消息
shu mei pai
Basics of reptile - Scratch reptile
Mex related learning
"Designer universe": "benefit dimension" APEC public welfare + 2022 the latest slogan and the new platform will be launched soon | Asia Pacific Financial Media
Data governance: misunderstanding sorting
"Designer universe" Guangdong responds to the opinions of the national development and Reform Commission. Primary school students incarnate as small community designers | national economic and Informa
Le chemin du navigateur Edge obtient
Type of data in energy dashboard
Key value judgment in the cycle of TS type gymnastics, as keyword use
22. Empty the table
Vit (vision transformer) principle and code elaboration
数据治理:数据质量篇
Compliance and efficiency, accelerate the digital transformation of pharmaceutical enterprises, and create a new document resource center for pharmaceutical enterprises
[KMP] template
Esrally domestic installation and use pit avoidance Guide - the latest in the whole network