当前位置:网站首页>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
边栏推荐
- R语言书籍学习03 《深入浅出R语言数据分析》-第八章 逻辑回归模型 第九章 聚类模型
- 206.反转链表
- Latex combat notes 3- macro package and control commands
- Two methods to judge the storage of large and small end
- Huawei equipment configuration hovpn
- Leetcode-32- longest valid bracket
- Rexroth overflow valve zdb6vp2-42/315v
- The network connection is normal, but Baidu web page can not be opened and displayed. You can't access this website solution
- R language book learning 03 "in simple terms R language data analysis" - Chapter 8 logistic regression model Chapter 9 clustering model
- 继承的所有特征
猜你喜欢

RPA super automation | nongnongji and cloud expansion accelerate financial intelligent operation

类和对象(1)

RPA+低代码助推品牌电商启新创变、重启增长
![[niuke.com] DP30 [template] 01 Backpack](/img/a2/9bcfbe6f78f30282fd8940c57477b1.jpg)
[niuke.com] DP30 [template] 01 Backpack

类和对象(4)

R language book learning 03 "in simple terms R language data analysis" - Chapter 7 linear regression model

Collection of articles and literatures related to R language (continuously updated)

On the night of the joint commissioning, I beat up my colleagues

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

科普 | NFT的类型有哪些(上)
随机推荐
RPA+低代码为何是加速财务数字化转型之利器?
每日一题-删除有序数组的重复项
[v2.1] automatic update system based on motion step API (repair bug, increase completion display, support disconnection reconnection and data compensation)
如何使用事物码 SAT 查找某个 SAPGUI 屏幕字段对应的后台存储数据库表的名称试读版
EndnoteX9簡介及基本教程使用說明
RPA丨首席财务官如何找到数字化转型“超级入口”?
Flink error: multiple tasks are started, and only one task is executed
Customer information management software
Addition without addition, subtraction, multiplication, Division
Leetcode-104- maximum depth of binary tree
All inherited features
The college entrance examination is over, and life has just begun. Suggestions from a 10-year veteran in the workplace
Leetcode-322- change exchange
Leetcode-98- validate binary search tree
Huawei equipment configuration hovpn
Leetcode-110-balanced binary tree
Take off efficiently! Can it be developed like this?
189. 轮转数组
Nmap进行主机探测出现网段IP全部存活情况分析
Parker plunger pump pv180r1k1t1nmmc