当前位置:网站首页>[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
边栏推荐
- 【微信授权登录】uniapp开发小程序,实现获取微信授权登录功能
- Intelligent operation and maintenance practice: banking business process and single transaction tracking
- [opencv450] hog+svm and hog+cascade for pedestrian detection
- 基于全志H3的QT5.12.9移植教程
- How can entrepreneurial teams implement agile testing to improve quality and efficiency? Voice network developer entrepreneurship lecture Vol.03
- 数据分析方法论与前人经验总结【笔记干货】
- export default 导出的对象,不能解构问题,和module.exports的区别
- Is it safe to buy funds in a securities account? Where can I buy funds
- Upgraded wechat tool applet source code for mobile phone detection - supports a variety of main traffic modes
- Barbie q! How to analyze the new game app?
猜你喜欢

Synthetic watermelon game wechat applet source code / wechat game applet source code

New version of free mobile phone, PC, tablet, notebook four terminal Website thumbnail display diagram online one click to generate website source code

Database -- sqlserver details

使用 ES 实现疫情地图或者外卖点餐功能(含代码及数据)

excel查找与引用函数

【底部弹出-选择器】uniapp Picker组件——底部弹起的滚动选择器

SQL Server Installation Guide

Using multithreaded callable to query Oracle Database

Xinniuniu blind box wechat applet source code_ Support flow realization, with complete material pictures

gradle
随机推荐
gradle
数据分析方法论与前人经验总结【笔记干货】
To meet the needs of consumers in technological upgrading, Angel water purifier's competitive way of "value war"
Guide d'installation du serveur SQL
Upgraded wechat tool applet source code for mobile phone detection - supports a variety of main traffic modes
SQL Server 安裝指南
程序员该如何更好的规划自己的职业发展?
I want to ask, which is the better choice for securities companies? I don't understand. Is it safe to open an account online now?
Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?
excel数据透视表
Use es to realize epidemic map or take out order function (including code and data)
Node——添加压缩文件
Source code of Qiwei automatic card issuing system
[leetcode] number of maximum consecutive ones
js 公共库 cdn 推荐
测试员8年工资变动,令网友羡慕不已:你一个月顶我一年工资
Excel PivotTable
JS -- image to base code, base to file object
工作中非常重要的测试策略,你大概没注意过吧
Slf4j print abnormal stack information