当前位置:网站首页>[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
边栏推荐
- 智能运维实战:银行业务流程及单笔交易追踪
- 一名优秀的软件测试人员,需要掌握哪些技能?
- Schrodinger's Japanese learning applet source code
- 【CTF】bjdctf_ 2020_ babystack2
- An intern's journey to cnosdb
- 【底部弹出-选择器】uniapp Picker组件——底部弹起的滚动选择器
- Data analysis methodology and previous experience summary [notes dry goods]
- The new version of graphic network PDF will be released soon
- 基于全志H3的QT5.12.9移植教程
- SQL数据分析之子查询的综合用法和案例题【耐心整理】
猜你喜欢

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

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

Source code of Qiwei automatic card issuing system

Collection: comprehensive summary of storage knowledge

sso单点登录的实现。

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

Output results of convolution operation with multiple tensors and multiple convolution kernels

ThreadLocal内存泄漏是什么,怎么解决

Leetcode skimming: stack and queue 04 (delete all adjacent duplicates in the string)

Leetcode skimming: stack and queue 06 (top k high-frequency elements)
随机推荐
AIX存储管理之逻辑卷的创建及属性的查看和修改
449 original code, complement code, inverse code
LeetCode 0241.为运算表达式设计优先级 - DFS
Data analysis methodology and previous experience summary [notes dry goods]
使用多线程Callable查询oracle数据库
2022 safety officer-a certificate examination questions and online simulation examination
股票开户哪个证券公司比较安全
What skills does an excellent software tester need to master?
449-原码、补码、反码
How to open an account for individual stock speculation? Is it safe?
UVM tutorial
Leetcode skimming: binary tree 03 (post order traversal of binary tree)
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?
Upgraded wechat tool applet source code for mobile phone detection - supports a variety of main traffic modes
2022 low voltage electrician examination questions and answers
Is it safe and reliable to open an account in Caixue school and make new debts?
基于全志H3的QT5.12.9移植教程
Common loss function of deep learning
Node——生成微信权限验证配置
You probably haven't noticed the very important testing strategy in your work