当前位置:网站首页>C language implements eight sorts (3)
C language implements eight sorts (3)
2022-06-11 21:57:00 【Code loving students】
3. Selection sort
3.1 Selection sort
What is selective sorting ?
Selection sort (Selection sort) It is a simple and intuitive sorting algorithm .
principle : Select the smallest data element from the data elements to be sorted for the first time ( Or maximum ) An element of , Store at the beginning of the sequence , And then find the smallest of the remaining unsorted elements ( Big ) Elements , Then put it at the end of the sorted sequence . And so on , Until the number of all data elements to be sorted is zero . Selective sorting is an unstable sorting method .
The code implementation is as follows :
void swap(int*a,int i,int j){
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
void SelectSort(int*a,int length){
int i,j;
for(i=0;i<length-1;i++){ // perform n-1 This cycle is because n-1 Sorting has been completed after times , There is no need for the last sort
int k=i;
for(j=i+1;j<length;j++){ // Compare from the next position
if(a[k]>a[j]){ // The subscript of the required value is recorded here
k=j;
}
}
if(k!=i){// If k==i Then it means that the current number is what I ask , There is no need to exchange
swap(a,k,i);
}
}
}3.2 Heap sort
Our blog also talked about piles before , Those who are not familiar with the heap can see our previous blog
The basic introduction of heap
We can use the special properties of heap to realize sorting
The code implementation is as follows :
void swap(int*a,int i,int j){
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
void HeapAdjust(int*a,int i,int length){
int rc=a[i];
for(int j=2*i+1;j<length;j=j*2+1){
if((j<length-1)&&a[j]<a[j+1]){ //j<length-1 Guarantee a[j+1] Do not cross the line
j++;
}
if(a[j]<rc){
break;
}
// If the root node is smaller than the child node, exchange
a[i]=a[j];
a[j]=rc;
i=j;
}
}
void HeapSort(int*a,int length){
int last=length-1;// The last element subscript
int parent=(last-1)/2;// Heap building from the last parent node , Because every leaf node is a heap
for(int i=parent;i>=0;i--){
HeapAdjust(a,i,length);
}
for(int i=length-1;i>0;i--){
swap(a,0,i); // Swap the first and last
HeapAdjust(a,0,i); // Readjust the heap
}
} We only need to exchange the first and last to complete the sorting .
边栏推荐
- Leetcode-322- change exchange
- Leetcode-32- longest valid bracket
- RPA丨首席财务官如何找到数字化转型“超级入口”?
- Top - k问题
- go os模块
- 189. 轮转数组
- Leetcode - 第2天
- 如何使用事物码 SAT 查找某个 SAPGUI 屏幕字段对应的后台存储数据库表的名称
- How does the chief financial officer of RPA find the "super entrance" of digital transformation?
- Latex combat notes 3- macro package and control commands
猜你喜欢

Leetcode-129- sum of numbers from root node to leaf node

ESP32C3 Arduino库使用方法

行而不辍,未来可期|云扩科技入选上海市专精特新企业

Popular science | what are the types of NFT (Part 1)
![[niuke.com] DP30 [template] 01 Backpack](/img/a2/9bcfbe6f78f30282fd8940c57477b1.jpg)
[niuke.com] DP30 [template] 01 Backpack

The upcoming launch of the industry's first retail digital innovation white paper unlocks the secret of full link digital success

实现栈和队列

189. rotation array

JVM | runtime data area; Program counter (PC register);

Introduction à endnotex9 et instructions pour les tutoriels de base
随机推荐
Take off efficiently! Can it be developed like this?
Leetcode-43- string multiplication
建造者模式
自定义实现offsetof
How to view the installation date of the win system
Example of using zypper command
EndnoteX9簡介及基本教程使用說明
C语言实现八种排序 - 归并排序
Classes and objects (1)
Why microservices are needed
Customer information management software
Go OS module
即将首发 | 业界首个零售数字化创新白皮书,解锁全链路数字化致胜秘籍
Huawei equipment configuration hovpn
RPA+低代码助推品牌电商启新创变、重启增长
每日一题 -- 验证回文串
Flink error: multiple tasks are started, and only one task is executed
Expérience 10 génération de courbes bezier - amélioration expérimentale - génération de courbes B - spline par point de contrôle
多态的所有特征
每日一题-删除有序数组的重复项