当前位置:网站首页>C language to achieve eight sorts (2)
C language to achieve eight sorts (2)
2022-06-11 21:57:00 【Code loving students】
2. Exchange sort
2.1 Bubble sort
What is bubble sorting ?
Bubble sort (Bubble Sort) It is also a simple and intuitive sorting algorithm .
It repeatedly visits the sequence to be sorted , Compare two elements at a time , If they're in the wrong order, exchange them . The job of the interview sequence is to repeat until there is no need to exchange , That is to say, the sequence has been sorted .
The name of this algorithm comes from the fact that the smaller the elements, the more slowly “ floating ” Go to the top of the list .
The code implementation is as follows :
void BubbleSort(int *a,int length){
int i,j;
int flag=1; // Determine whether to cycle
for(i=0;i<length-1&&flag==1;i++){
for(j=0;j<length-1-i;j++){
flag=0;
if(a[j]>a[j+1]){
flag=1;
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}However, due to the high time complexity of bubble sorting , All this leads to a new sort of sorting -- Quick sort (QuickSort).
The name can show its speed .
How is this sort realized ?
The code is as follows :
// Quick sort
int Partition(int*a,int left,int right){
int temp=a[left];//left Subscript as reference number
while(left<right){
while(left<right&&a[right]>temp){
right--;
}
if(left<right){
a[left]=a[right];
}
while(left<right&&a[left]<temp){
left++;
}
if(left<right){
a[right]=a[left];
}
}
a[right]=temp;
return right;
}
void QuickSort(int*a,int left,int right){
if(left<right){
int pivot=Partition(a,left,right);
QuickSort(a,left,pivot-1);
QuickSort(a,pivot+1,right);
}
}We first choose one as the reference number , And then iterate through the entire array ( In addition to the benchmark ), Place less than the reference number on its left , Those larger than the reference number are placed on its right .
Then the left and right sides of the reference number are regarded as an array , Repeat the above operation , Until there is only one element left in each array .

Let me have another water
边栏推荐
- 如何使用事物码 SAT 查找某个 SAPGUI 屏幕字段对应的后台存储数据库表的名称试读版
- Leetcode-155-minimum stack
- 类和对象(2)
- 建造者模式
- R language book learning 03 "in simple terms R language data analysis" - Chapter 10 association rules Chapter 11 random forest
- 类和对象(1)
- 相对完善的单例模式
- Go IO module
- 动态内存管理(1)
- Superscalar processor design yaoyongbin Chapter 2 cache -- Excerpt from subsection 2.4
猜你喜欢

R语言书籍学习03 《深入浅出R语言数据分析》-第八章 逻辑回归模型 第九章 聚类模型

win11怎么看电脑显卡信息

Introduction à endnotex9 et instructions pour les tutoriels de base

RPA+低代码助推品牌电商启新创变、重启增长

Leetcode-155-minimum stack

【历史上的今天】6 月 11 日:蒙特卡罗方法的共同发明者出生;谷歌推出 Google 地球;谷歌收购 Waze

Why is rpa+ low code a powerful tool to accelerate the digital transformation of finance?

Carry and walk with you. Have you ever seen a "palm sized" weather station?

How to view the installation date of the win system

科普 | NFT的类型有哪些(上)
随机推荐
每日一题 - 罗马数字转整数
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
类和对象(1)
LabVIEW controls Arduino to realize infrared ranging (advanced chapter-6)
Nmap performs analysis of all network segment IP survivals in host detection
R语言书籍学习03 《深入浅出R语言数据分析》-第十二章 支持向量机 第十三章 神经网络
相对完善的单例模式
超标量处理器设计 姚永斌 第2章 Cache --2.3 小节摘录
为什么需要微服务
JVM class loader; Parental delegation mechanism
类和对象(2)
[niuke.com] DP30 [template] 01 Backpack
[academic related] under the application review system, how difficult is it to study for a doctoral degree in a double first-class university?
go encoding包
Latex combat notes 3- macro package and control commands
Superscalar processor design yaoyongbin Chapter 2 cache -- Excerpt from subsection 2.3
[niuke.com] dp31 [template] complete Backpack
Superscalar processor design yaoyongbin Chapter 2 cache -- Excerpt from subsection 2.2
Go IO module
The college entrance examination is over, and life has just begun. Suggestions from a 10-year veteran in the workplace