当前位置:网站首页>Several common sorts
Several common sorts
2022-07-24 04:40:00 【Yearn for mysterious paradise】
Sort
Sort : Sort , Is to make a string of records , According to the size of one or some of the keywords , An operation arranged in ascending or descending order , There are many applications of sorting in life , Such as the product ranking of a treasure , Ranking of institutions, etc .
Common sorting is divided into : Insertion sort 、 Selection sort 、 Exchange sort 、 Merge, sort, etc , Insert sort includes direct insert sort and Hill sort , Select sort contains : Selection sort 、 Heap sort , The exchange sort includes : Bubble sort 、 Quick sort , Merge sort contains itself .
This time mainly introduces heap sorting in insert sorting and select sorting , The idea of inserting sorting is relatively simple , Its basic idea is like playing cards at ordinary times , Insert the disordered cards from large to small or from small to large into the orderly playing cards one by one , Until all the cards are in order , A more detailed description is as follows : When inserting the i(i>=1) Element time , Ahead array[0],array[1],…,array[i-1] It's in order , Use at this time array[i] The sort code of and array[i-1],array[i-2],… Compare the sort code order of , Find the insertion position array[i] Insert , The elements in the original position move backward in order .
Its main code is as follows :
void InserSort(int* a, int n)
{
for (int i = 0;i < n-1;++i)
{
int end = i;
int tmp = a[end + 1];
while (end >= 0)
{
if (a[end] > tmp)
{
a[end + 1] = a[end];
--end;
}
else
{
break;
}
}
a[end + 1] = tmp;
}
}
To understand first while What's in it , It is equivalent to a single insertion sort , hypothesis end The position is 3, Then start with the third number of the array , from end Subscript to the array 0 The position of... Approaches one by one , if a[end]>a[end+1] Words , take end Move the number of positions back one , then end–, Is to approach the array step by step, and the subscript is 0 The process of , Until before end The number of is ordered , and for The process of statement is equivalent to before control n An orderly process , This is the process of direct insertion , But the time complexity of direct insertion is O(N^2), Because in the worst case of the array ( The array is in reverse order , At this time, ascending order is required ), Therefore, in view of this situation , Hill proposed an improved direct insertion sorting method , Hill sort .
The process of Hill sort is from pre sort to direct sort , In the process of pre sorting , Use the method of space sorting , Use gap To represent the size of the entire interval , Its pre sorting idea is consistent with direct insertion sorting , The difference is , Direct insertion sorting is to compare two adjacent numbers , Then move the larger one back ( In ascending order , The descending order is the opposite ), The pre sorting process is interval gap Compare the two numbers of , The larger one moves back , When gap by 1 when , It is direct insertion sorting ,gap The bigger the value is. , The larger the number, the faster it will reach the end ( In ascending order ), But the less it approaches order ,gap The smaller it is , The closer it is to order , But the larger the number, the slower it reaches the end , The following figure shows the different gap Under value , Different states of array sorting .
The code for Hill's sorting is as follows :
void ShellSort(int* a, int n)
{
int gap = n;
while (gap > 1)
{
gap = gap / 3 + 1;
for (int i = 0;i < n - gap;++i)
{
int end = i;
int tmp = a[end + gap];
while (end >= 0)
{
if (a[end] > tmp)
{
a[end + gap] = a[end];
end -= gap;
}
else
{
break;
}
}
a[end + gap] = tmp;
}
}
}
Hill sort through gap To control the pre sorting process , When gap The value is 1 when , The sort is direct insert sort , Finally finish sorting the array , The time complexity of hill sorting is O(n^1.5) about , The calculation of time complexity is cumbersome , There will be no discussion here , In the future, I will continue to share several other sorting ideas , And discuss its time complexity , And stability .
边栏推荐
- Is it true to pay attention to official account and receive Xiaomi mobile power for free? Wechat circle of friends sends Xiaomi mobile power
- Introduction to the application fields and functions of bank virtual human technology
- To 3mm; Provide safe and stable product execution according to the sender IID
- Face algorithms
- Why can't I log on my baidu account? Introduction to the solution of baidu account unable to log in
- Nautilus 3.19.2为Gnome增添动力
- NFT除了买卖还能质押?
- pycharm 调试功能介绍与使用
- 你有多久没有换新手机了?
- 归并排序(Merge sort)
猜你喜欢

Application scenarios and schemes of common mechanical equipment safety virtual simulation system

Nautilus 3.19.2为Gnome增添动力

高频小信号谐振放大器设计-课程设计Multisim仿真

The second anniversary of opengauss' open source, cracking the pain point of database ecology

The judges of C language classic exercises score the highest and lowest to get an average score

Robustness evaluation of commercial in vivo detection platform

Label smoothing

Little black leetcode journey: 100 same trees

A hospital call system based on C language

数组力扣(持续更新)
随机推荐
一次线上事故,我顿悟了异步的精髓
Airiot Q & A issue 5 | how to use low code business flow engine?
C语言:冒泡排序法
归并排序(Merge sort)
How to get the signature file of Baidu Post Bar? Baidu Post Bar signature file setting and use method graphic introduction
高频小信号谐振放大器设计-课程设计Multisim仿真
C语言:选择排序法
J9 number theory: what is Web3.0? What are the characteristics of Web3.0?
[2023 core technology approval test questions in advance] ~ questions and reference answers
Imitate today's headlines real-time news wechat applet project source code
LabVIEW主VI冻结挂起
eCB接口,其实质也 MDSemodet
《论文复现》BiDAF代码实现过程(3)模型建立
How much do you know about thread pool and its application
Gau and ppm for image semantic segmentation
佳的性能和可靠性发起写入IIC协类型码和的参数是-4
Learn more about the new features of ES6 in grain mall of e-commerce project
Baidu wallet helps you repay the inter-bank repayment of your credit card. The handling fee is 0. Newcomers who arrive in real time will be rewarded with 5 yuan
What is the proper resolution of the computer monitor? Introduction to the best resolution of monitors of various sizes and the selection of different wallpapers
Export function called separately