当前位置:网站首页>Gnawing down the big bone - sorting (I)
Gnawing down the big bone - sorting (I)
2022-06-29 22:42:00 【Let everything burn】
List of articles
1. The concept of sorting and its application
1.1 The concept of sequencing
The essence of sorting is to filter , Life is inseparable from sequencing
Sort : So called sorting , 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 .
stability : Assume in the sequence of records to be sorted , There are multiple records with the same keyword , If sorted , The relative order of these records remains the same , In the original sequence ,r[i]=r[j], And r[i] stay r[j] Before , In the sorted sequence ,r[i] Still in r[j] Before , We call this sort algorithm stable ; Otherwise it is called unstable .
Internal sorting : Sorting in which all data elements are placed in memory .
External sorting : Too many data elements to be in memory at the same time , According to the requirements of the sorting process, data sorting cannot be moved between internal and external storage .
1.2 Sorting application
When buying things, the sales volume is sorted , Comprehensive ranking , Good reviews, etc
University ranking, etc
Video recommendation stream , Recommendation algorithm Leaderboard with goods
1.3 Common sorting algorithms

2. Implementation of common sorting algorithms
2.1 Insertion sort
2.1.1 The basic idea :
Direct insertion sort is a simple insertion sort method , Its The basic idea yes :
Insert the records to be sorted into an ordered sequence one by one according to their key values , Until all the records are inserted , We get a new ordered sequence .
In practice, we play poker ( Fight the landlord and manage the cards ) when , We use the idea of insertion sort
Look at... In ascending order , The descending order is the reverse
Orderly :
2 6 7 9
Number to be inserted : 10 5 0
A single trip is orderly : Ordered interval , Inserting a number is still in order
2.1.2 Direct insert sort :
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
Summary of the features of direct insertion sort :
- The closer the set of elements is to order , The more time efficient the direct insertion sort algorithm is
- Time complexity :O(N^2)
The optimal : Orderly or near orderly O(N)
The worst : The reverse - Spatial complexity :O(1), It's a stable sort algorithm
- stability : Stable
// Direct insert sort
void InsertSort(int* a, int n)
{
// A loop is a whole sequence
//n-2 It's the penultimate position , Because it is the end+1 Insert in front , therefore end It's not worth it for The cycle stops
// Array subscript is from 0 At the beginning
for (int i = 0; i < n - 1; ++i)
{
// Ideas :[0,end] Orderly , hold end+1 The value of the position is inserted , Still in order
int end = i;
// Define a tmp Variable , Store the value you are about to insert
int tmp = a[end + 1];// hold end The latter one is inserted into the front
// Single pass sorting
while (end >= 0)
{
if (tmp < a[end])
{
a[end + 1] = a[end];
--end;
}
else
{
// If the inserted value is 0,-- To -1 First jump out and then assign value , Otherwise 0 Will fail to insert
break;
}
}
a[end + 1] = tmp;
}
}
Be careful: Its sort is not directly exchangeable , It's a end+1 Insert into the previous ordered sequence
2.1.3 Shell Sort ( Reduced delta sort )
Hill ranking is also known as
Reduction increment method.
Hill's ranking method The basic idea yes :
- Choose an integer first , Divide all the records in the file to be sorted into groups , All records at a distance of are in the same group , And sort the records in each group .
- then , take , Repeat the above work of grouping and sorting . When they arrive in =1 when , All records are arranged in a unified group .
Ideas : Pre sort ( Close to order ) Group insertion prearrangement , Divide into gap Group ( Spacing is gap Is a set of data ), For these gap Insert and sort the group data separately Direct insert sort ( Orderly )
Hill sorted Feature summary :
Hill sort is an optimization of direct insert sort .
When
gap > 1It's all pre sorted , The goal is to make arrays more ordered .
Whengap == 1when , The array is nearly ordered , This will soon .
So on the whole , Can achieve the effect of optimization . We can compare the performance test after implementation .The time complexity of Hill sort is not easy to calculate , because gap There are many ways to get the value of , Makes it difficult to calculate , Therefore, the time complexity of hill sorting given in many trees is not fixed :
// Shell Sort
void ShellSort(int* a, int n)
{
// When one group is finished, the next group
// // Control multiple groups
// int gap = 3;
// //gap There are several groups of data
// for (int j = 0; j < gap; ++j)
// {
// for (int i = j; i < n - gap; i += gap)
// {
// // Single trip , The same idea as direct insertion sorting
// int end = i;
// int tmp = a[end + gap];
//
// while (end >= 0)
// {
// if (tmp < a[end])
// {
// a[end + gap] = a[end];
// end -= gap;
// }
// else
// {
// break;
// }
// }
// a[end + gap] = tmp;
// }
// }
//
// simplify
// One after another , Groups walk alternately
int gap = n;
//gap>1 It is pre sort
//gap The last time is equal to 1, Is to insert sort directly ( Keep order )
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 (tmp < a[end])
{
a[end + gap] = a[end];
end -= gap;
}
else
{
break;
}
}
a[end + gap] = tmp;
}
}
}
Be careful :
gap The bigger it is , Big numbers get to the back faster , Small numbers go faster to the front , But the less close to order
gap The smaller it is , The closer to order
When gap be equal to 1 when , Direct sorting
2.2 Selection sort
2.2.1 The basic idea :
Each time, select the smallest from the data elements to be sorted ( Or maximum ) An element of , Store at the beginning of the sequence , Until all the data elements to be sorted are finished .
2.2.2 Direct selection sorting :
Ideas :
Select the first number , Then use traversal to compare the following numbers in turn , If there is something smaller than the first number, exchange it , If there is nothing smaller than the number to be compared, traverse the smallest of the remaining numbers and put them in front
Go through it and choose
Go through it and choose the smallest one first , Go through it again and choose the smaller one , In turn
In the element set array[i]–array[n-1] Select the largest key in ( Small ) Data elements
If it's not last of the these elements ( first ) Elements , Then it is combined with the last element in the group ( first ) Element exchange
In the remaining array[i]–array[n-2](array[i+1]–array[n-1]) Collection , Repeat the above steps , Until the set remains 1 Elements
Be careful : Looking for subscript , swapping
Select the sorted directly Feature summary :
- It's very easy to understand how to think directly , But the efficiency is not very good . It is seldom used in practice
- Time complexity :O(N^2)
- Spatial complexity :O(1)
- stability : unstable
void Swap(int* p1, int* p2)
{
int tmp = *p1;
*p1 = *p2;
*p2 = tmp;
}
// Direct selection sorting
void SelectSort(int* a, int n)
{
assert(a);
//begin,end It's an array subscript [] Number in Li , Subscript from 0 Start , So the last one is n-1
int begin = 0, end = n - 1;
// When will it come to an end? : An odd number of encounters , Even missed
while (begin < end)
{
// The starting position is given to the first number , Then start with the second number
int mini = begin, maxi = begin;
for (int i = begin + 1; i <= end; ++i)
{
// Traverse , contrast ,
if (a[i] < a[mini])
// to update
mini = i;
if (a[i]>a[maxi])
maxi = i;
}// At this point, the maximum and minimum values have been found , Store separately maxi And mini in
// In exchange for
Swap(&a[begin], &a[mini]);
// If begin and maxi overlap , Need to fix
if (begin == maxi)
{
maxi = mini;
}
Swap(&a[end], &a[maxi]);
// At this time, it only traverses once , It needs to be traversed again
++begin;
--end;
}
}
And
Heap sortcontrast : O(N^2) The advantages are simple , But it's slowAnd
Direct insert sortcontrast : It's better to insert ( It's just that the ranking advantage with hill is weak )
Selection sorting is almost the slowest It is best to N^2,
The slowest N^2
2.2.3 Heap sort
Heap sort (Heapsort) It's the use of stacked trees ( Pile up ) A sort algorithm designed by this data structure , It's a sort of selective sort .
It selects data through the heap . It's important to note that in ascending order you have to build lots of , Build small piles in descending order .
Select the sorted directly Feature summary :
- Heap sort uses heap to select numbers , It's a lot more efficient .
- Time complexity :O(N*logN)
- Spatial complexity :O(1)
- stability : unstable
void AdjustDwon(int* a, int size, int parent)
{
int child = parent * 2 + 1;
while (child < size)
{
if (child + 1 < size && a[child + 1] > a[child])
{
++child;
}
if (a[child] > a[parent])
{
Swap(&a[child], &a[parent]);
parent = child;
child = parent * 2 + 1;
}
else
{
break;
}
}
}
// Descending -- Build a small pile
// Ascending -- Build a pile
void HeapSort(int* a, int n)
{
for (int i = (n-1-1)/2; i >= 0; --i)
{
AdjustDwon(a, n, i);
}
// O(N*logN)
int end = n - 1;
while (end > 0)
{
Swap(&a[0], &a[end]);
AdjustDwon(a, end, 0);
--end;
}
}
边栏推荐
- 5-2web application vulnerability scanning
- 啃下大骨头——排序(一)
- The details of industry are all made by money and time
- qt5.14.2连接ubuntu20.04的mysql数据库出错
- math_基本初等函数图型(幂函数/指数/对数/三角/反三角)
- 软件测试方法和技术知识点有哪些?
- LeetCode85+105+114+124
- The correct method for Navicat to connect to mysql8.0 (valid for personal testing)
- 26 years old, 0 basic career change software test, from 3K to 16K monthly salary, a super complete learning guide compiled by me
- static关键字续、继承、重写、多态
猜你喜欢
Why does copying files on a shared folder on a local area network (ERP server) result in the loss of the local Internet

【多线程】 如何自己实现定时器

从零实现深度学习框架——LSTM从理论到实战【理论】

Matplotlib histogram

5分钟快速上手 pytest 测试框架

Dynamics 365online lookup lookup field multiple selection

还天天熬夜加班做报表?其实你根本不懂如何高效做报表

Mysql入库不了表情符号怎么办

稳!上千微服务接入 Zadig 的最佳姿势(Helm Chart 篇)

低代码、端到端,一小时构建IoT示例场景,声网发布灵隼物联网云平台
随机推荐
Ce CDC Flink peut - il être utilisé pour la synchronisation incrémentale d'Oracle à MySQL?
If I am in Zhuhai, where can I open an account? Is it safe to open an account online?
Day9 ---- 用户注册与登录
联通入库|需要各地联通公司销售其产品的都需要先入总库
Why does copying files on a shared folder on a local area network (ERP server) result in the loss of the local Internet
【无工具搭建PHP8+oracle11g+Windows环境】内网/无网络/Win10/PHP连接oracle数据库实例
Number theory - division and blocking
啃下大骨头——排序(一)
一键式文件共享软件Jirafeau
5-2web application vulnerability scanning
LeetCode85+105+114+124
中国数据库崛起,阿里云李飞飞:中国云数据库多种主流技术创新已领先国外
5-1系统漏洞扫描
阶段性总结与思考
Numpy array creation
Build a short video platform, fade in and fade out, and support left sliding and right pulley to broadcast pictures
Arrange the array into the smallest number_ Reverse pairs in an array (merge Statistics)_ Number of occurrences of a number in an ascending array_ Ugly number (Sword finger offer)
在线文本数字识别列表求和工具
Does Australia require that PVC plastic sheets comply with as/nzs 1530.3 with a flame spread index of 0?
Summary of basic concepts of moosefs