当前位置:网站首页>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
边栏推荐
- [redis] Introduction to NoSQL database and redis
- Risk planning and identification of Oracle project management system
- Comparison of usage scenarios and implementations of extensions, equal, and like in TS type Gymnastics
- 数据治理:主数据的3特征、4超越和3二八原则
- Circuit breaker: use of hystrix
- Make learning pointer easier (3)
- How to estimate the number of threads
- PHP Coding Standard
- (lightoj - 1410) consistent verbs (thinking)
- Luogu p1836 number page solution
猜你喜欢
Google may return to the Chinese market after the Spring Festival.
opencv学习笔记八--答题卡识别
esRally国内安装使用避坑指南-全网最新
Nft智能合约发行,盲盒,公开发售技术实战--拼图篇
Golang DNS 随便写写
【T31ZL智能视频应用处理器资料】
National economic information center "APEC industry +": economic data released at the night of the Spring Festival | observation of stable strategy industry fund
Comparison of usage scenarios and implementations of extensions, equal, and like in TS type Gymnastics
octomap averageNodeColor函数说明
链表面试题(图文详解)
随机推荐
Asia Pacific Financial Media | art cube of "designer universe": Guangzhou community designers achieve "great improvement" in urban quality | observation of stable strategy industry fund
What are the ways to download network pictures with PHP
JS select all and tab bar switching, simple comments
【云原生】手把手教你搭建ferry开源工单系统
Esrally domestic installation and use pit avoidance Guide - the latest in the whole network
C # create database connection object SQLite database
opencv学习笔记九--背景建模+光流估计
A Closer Look at How Fine-tuning Changes BERT
On why we should program for all
Description of octomap averagenodecolor function
National economic information center "APEC industry +": economic data released at the night of the Spring Festival | observation of stable strategy industry fund
The State Economic Information Center "APEC industry +" Western Silicon Valley will invest 2trillion yuan in Chengdu Chongqing economic circle, which will surpass the observation of Shanghai | stable
Database basic commands
数据治理:误区梳理篇
How to prevent Association in cross-border e-commerce multi account operations?
Risk planning and identification of Oracle project management system
软件开发的一点随记
数据治理:元数据管理篇
【T31ZL智能视频应用处理器资料】
wincc7.5下载安装教程(Win10系统)