当前位置:网站首页>Selective sort method
Selective sort method
2022-06-24 23:26:00 【Short section senior】
Simple selection sort
The basic idea
Each time, select a record with the smallest keyword as the current record to be determined .
Algorithmic thought
The first trip : Select a record with the smallest keyword , And exchange with the first record .
The second trip : In the rest of the sequence , Select a record with the smallest keyword , And exchange with the second record .
Repeat total n-1 You can finish sorting in one time .
The first i Trip to , Make sure No i A record
k Is the subscript of the maximum value
j Subscript for search 











Algorithm analysis
The best situation : Orderly , Comparison times n(n-1)/2, Number of moves 0, The time complexity is O(n2).
The worst : The first record has the largest keyword , Other records are in order , Comparison times n(n-1)/2, Number of moves 3(n-1), The time complexity is O(n2).
Spatial complexity :O(1).
stability : unstable .
Tree selection sort
Key points of algorithm improvement
Simple selection sort : from n To find the record with the smallest keyword among the elements, you need to compare n-1 Time .
From the rest n-1 Whether it is necessary to find the record with the second smallest keyword in the records n-2 This comparison ?
improvement : Save the size relationship in the comparison process . Requires space overhead .
Algorithmic thought
First, sort the n Compare the keywords of two records , Take out the smaller ones , And then in the selected n/2 Of the smaller , Then compare the smaller one , So again and again , Until the smallest keyword is selected .
Select the secondary keyword for , Set the keyword value of the leaf node corresponding to the minimum key record to ∞, Then compare the keyword of the leaf node with that of its sibling node , Modify the value from the leaf node to each node on the root path , Then the value of the root node is the minor keyword .
Select the minimum keyword 27



Algorithm analysis
set up n A leaf record , Depth is h Full binary tree .
In addition to selecting the minimum keyword , The selection of other smaller keywords needs to be compared h-1 Time , The comparison times for each keyword selected are log2n .
The time complexity of the comparison is O(nlog2n).
The number of times the record is moved does not exceed the number of comparisons , Therefore, the total time complexity is O(nlog2n).
The space complexity is O(n). Added n-1 Nodes .
stability : Stable . When the left and right children are equal , Give priority to the left child , Ensure that the priority relationship remains unchanged .
Heap sort
Key points of algorithm improvement
Tree sorting : Auxiliary space n-1 Record size .
Heap order : Auxiliary space 1 Record size .
The concept of heaps
A complete binary tree is a sequential table ( Array mode ) Storage , Each node ( That is, each record ) Keywords of meet the conditions :r[i].key>=r[2i].key also r[i].key>=r[2i+1].key A complete binary tree of is called a large root heap .
That is, the keyword of the node of the large root heap is greater than or equal to the keyword of its left and right children .
conversely , It's called a root heap .
Heap sort is an application of the complete binary tree sequential structure .
example : Determine whether the following two sequences are heaped ?
{ 98,77,35,62,55,14,35,48 }
{ 14,48,35,62,55,98,35,77 }
Rebuild heap
Problem solved : When the top record changes , How to rebuild the heap .
Method :“ Screening ” Law .
Auxiliary space : A record size space .
for example : Known initial sequence of keywords { 98,77,35,62,55,
14,35,48 }, The corresponding is shown in the figure below . After exchanging the top record with the bottom record , Give the adjustment and build process of the remaining records .
Top tail exchange , Before remaining n-1 A record , Determined the n Records with the largest keyword .







Jianchu pile
Problem solved : Adjust any sequence to heap .
Algorithmic thought : A single node binary tree is a heap . The sequence number of leaf node is greater than n/2.
“ Screening ” Just start from page n/2 Start with a node , Until the root node .
for example : Known initial sequence of keywords { 48,62,35,77,55,
14,35,98 }, Adjust it to heap .





Heap sort
Problem solved : How to use heap to complete sorting .
It is known that : The top and bottom records of the heap are exchanged , Before remaining n-1 A record , Determined the n Records with the largest keyword .
Algorithmic thought :
① Jianchu pile .
② Exchange top and tail records , And rebuild the new heap with the remaining previous records .
③ Repeat step n-1 You can complete the sorting .






Algorithm analysis
Time complexity :O(nlog2n).
Spatial complexity :O(1).
stability : unstable .
Welcome to join me for wechat exchange and discussion ( Please note csdn Add )
边栏推荐
- 372. 棋盘覆盖
- Laravel message queue
- R language uses the multinom function of NNET package to build an unordered multi classification logistic regression model, and uses exp function and coef function to obtain the corresponding odds rat
- [JS] - [array application] - learning notes
- R语言dplyr包group_by函数和summarise_at函数计算dataframe计算不同分组的计数个数和均值(Summarise Data by Categorical Variable)
- R语言dplyr包select函数将dataframe数据中的指定数据列移动到dataframe数据列中的第一列(首列)
- Online group chat and dating platform test point
- 华为机器学习服务语音识别功能,让应用绘“声”绘色
- Blogs personal blog project details (servlet implementation)
- 02_ Springboot starter case
猜你喜欢

点的螺旋距离
![[JS] - [array, stack, queue, linked list basics] - Notes](/img/c6/a1bd3b8ef6476d7d549abcb442949a.png)
[JS] - [array, stack, queue, linked list basics] - Notes
How should we measure agile R & D projects?

idea创建模块提示已存在

【UVM入门 ===> Episode_8 】~ Sequence 和 Sequencer、Sequence 层次化

【js】-【树】-学习笔记

Blogs personal blog project details (servlet implementation)

【js】-【数组、栈、队列、链表基础】-笔记

Laravel pagoda security configuration

【js】-【数组应用】-学习笔记
随机推荐
慕思股份深交所上市:靠床垫和“洋老头”走红 市值224亿
03_ Spingboot core profile
golang convert json string to map
376. 機器任務
Construction equipment [4]
Theoretical analysis of countermeasure training: adaptive step size fast countermeasure training
常用正则表达式
idea创建模块提示已存在
【js】-【數組、棧、隊列、鏈錶基礎】-筆記
Financial management [3]
从客户端到服务器
[basic knowledge] ~ half adder & full adder
257. 关押罪犯
Building Survey [1]
二分查找数组下标
R language uses GLM function to build Poisson log linear regression model, processes three-dimensional contingency table data to build saturation model, uses summary function to obtain model summary s
Websocket long link pressure test
OpenSSL SSL_read: Connection was reset, errno 10054
QT to place the form in the lower right corner of the desktop
sql -CONVERT函数