当前位置:网站首页>[eight sorts ②] select sort (select sort, heap sort)
[eight sorts ②] select sort (select sort, heap sort)
2022-07-02 00:44:00 【Living_ Amethyst】
Catalog
【 Summary of the characteristics of direct selection sorting 】
Basic idea of heap sort algorithm
【 Summary of the characteristics of direct selection sorting 】
One 、 Selection sort
About selection and sorting
Selection sort is a simple and intuitive sorting algorithm , No matter what data goes in, it's O(n²) Time complexity of . So when you use it , The smaller the data, the better . The only benefit may be that it doesn't take up extra memory space .
Algorithm steps :
- First, find the minimum in the unsorted sequence ( Big ) Elements , To the beginning of the collating sequence .
- Continue to find the smallest from the remaining unsorted elements ( Big ) Elements , And then at the end of the sorted sequence .
- Repeat step 2 , Until all the elements are sorted .

Code implementation
/**
* Selection sort
* @param array
*/
public static void selectSort(int[]array){
for(int i = 0;i < array.length-1 ;i++) {
int minIndex = i;
for (int j = i + 1; j < array.length; j++) {
if (array[j] < array[minIndex]) {
minIndex = j;
}
}
if (minIndex != i) {
swap(array, minIndex, i);
}
}
}【 Summary of the characteristics of direct selection sorting 】
- 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
Two 、 Heap sort
We have already introduced the big root heap 、 The concept of small root pile , And heap sorting is to use heap ( Build a large root pile in ascending order , Build small piles in descending order ) The method of sorting .
Basic idea of heap sort algorithm
Construct the sequence to be sorted into a large root heap . At this time, the whole sequence Maximum Is the root node at the top of the heap . Remove it ( In fact, that is Swap it with the end element of the heap array , At this point, the last element is the maximum value ), Then I'll take the rest n-1 A sequence of Restructure into a heap , So you get n The next largest of the elements . Do it over and over again , Then we can get an ordered sequence

The above dynamic picture comes from elsewhere
Now let's disassemble each step
Pictured , It's a big pile ,90 Is the maximum , take 90 And 20( End element ) swap

here 90 It becomes the last element of the whole heap sequence

take 20 After adjustment , To divide 90 Other nodes continue to meet the definition of large root heap ( All nodes are larger than their children )

I believe you understand the basic idea of heap sorting , But to complete the heap sorting , need Solve the following two problems :
- How to build a heap from an unordered sequence ?
- If after outputting the top element , Adjust the remaining elements into a new heap ?
Let's look at another picture and experience ( Source elsewhere )
Let's look at the code
/**
* Heap sort
* @param array
* @param root
* @param len
*/
public static void shiftDown(int[]array,int root,int len){
int parent = root;
int child = (2*parent)+1;
while(child < len) {
if(child+1 < len && array[child] < array[child + 1]) {
child++;
}
if(array[child] > array[parent]){
swap(array,child,parent);
parent = child;
child = (2*parent)+1;
}else{
break;
}
}
}
public static void createHeap(int[] array){
for(int p = (array.length-1-1)/2; p>=0 ; p--){
shiftDown(array,p, array.length);
}
}
public static void heapSort(int[] array){
createHeap(array);
int end = array.length-1;
while(end>=0){
swap(array,0,end);
shiftDown(array,0,end);
end--;
}
}【 Summary of the characteristics of direct selection sorting 】
- Heap sort uses heap to select numbers , It's a lot more efficient .
- Time complexity :O(N*logN)
- Spatial complexity :O(1)
- stability : unstable
边栏推荐
- Excel PivotTable
- Common loss function of deep learning
- Collection: comprehensive summary of storage knowledge
- EMC circuit protection device for surge and impulse current protection
- LeetCode 0241.为运算表达式设计优先级 - DFS
- gradle
- heketi 记录
- King combat power query renamed toolbox applet source code - with traffic main incentive advertisement
- 2023款雷克萨斯ES产品公布,这回进步很有感
- Summary of Aix storage management
猜你喜欢

Mysql database driver (JDBC Driver) jar package download

RFID让固定资产盘点更快更准

Otaku wallpaper Daquan wechat applet source code - with dynamic wallpaper to support a variety of traffic owners

SQL Server Installation Guide

heketi 记录

九州云与英特尔联合发布智慧校园私有云框架,赋能教育新基建

SQL数据分析之子查询的综合用法和案例题【耐心整理】
![[wechat authorized login] the small program developed by uniapp realizes the function of obtaining wechat authorized login](/img/c1/23be4399119f42d85a7b86fc8a59fc.png)
[wechat authorized login] the small program developed by uniapp realizes the function of obtaining wechat authorized login

数据分析方法论与前人经验总结【笔记干货】

Picture puzzle wechat applet source code_ Support multi template production and traffic master
随机推荐
【js通过url下载文件】
JS——图片转base码 、base转File对象
Using multithreaded callable to query Oracle Database
4. Object mapping Mapstercover
Practical calculation of the whole process of operational amplifier hysteresis comparator
2023 Lexus ES products have been announced, which makes great progress this time
SSO single sign on implementation.
Common loss function of deep learning
Node——Egg 实现上传文件接口
SQL数据分析之窗口排序函数rank、dense_rank、raw_number与lag、lead窗口偏移函数【用法整理】
JMeter做接口测试,如何提取登录Cookie
2023款雷克萨斯ES产品公布,这回进步很有感
ThreadLocal内存泄漏是什么,怎么解决
数据库--SqlServer详解
Viewing and modifying volume group attributes of Aix storage management (II)
How can entrepreneurial teams implement agile testing to improve quality and efficiency? Voice network developer entrepreneurship lecture Vol.03
S32Kxxx bootloader之UDS bootloader
Summary of Aix storage management
gradle
【会议资源】2022年第三届自动化科学与工程国际会议(JCASE 2022)