当前位置:网站首页>Bubble sort, quick sort
Bubble sort, quick sort
2022-07-24 01:00:00 【lhb2998658795】
One . Bubble sort
1 The basic idea : Compare two adjacent numbers , Exchange according to rules .
2. Realize the idea :
( Take ascending order as an example ) The first comparison : First compare the first and second elements , Switch the larger one to the second position , Then compare the second and third , Put the larger one in the third position ... By analogy , At the end of the first comparison , The largest element is in the last position Then make a second comparison : First compare the first and second elements , Switch the larger one to the second position , Then compare the second and third , Put the larger one in the third position ... By analogy , Because the first comparison has put the largest in the last position So we can compare one element less in the second trip , At the end of the second trip , The second largest element is placed in the penultimate position By analogy , Until the last comparison is completed , The entire array is sorted . ( On the last trip , There is only one element left , Can not participate in the comparison )
3. Realize dynamic graph

4 Code implementation
#include <stdio.h>
int main(int argc, const char *argv[])
{
int s[10] = {23,45,65,78,90,55,33,17,96,54};
int i = 0;
int j = 0;
int temp = 0;
int flags=0;// This is set to improve efficiency
int len = sizeof(s)/sizeof(int);
for(i = 0; i < len; i++){
printf("%d ", s[i]);
}
printf("\n");
for(j = 0; j < len-1; j++){
flags=0;
for(i = 0; i < len-1-j; i++){
if(s[i] > s[i+1]){
temp = s[i];
s[i] = s[i+1];
s[i+1] = temp;
flags=1;
}
}
if(flags==0){
break;
}
}
for(i = 0; i < len; i++){
printf("%d ", s[i]);
}
printf("\n");
return 0;
}
Two . Quick sort
1 Concept
Quick sort is the optimization of bubble sort , It is also a sort method based on Exchange .
Time complexity O(nlogn).
2 The basic idea
Divide and rule through a sequence , First, divide the data into two parts , Part of the data , Are larger than the data of another part ( Small ),( Every part of the data is not required to be orderly ) then , Perform the above sorting operations for the two parts of data , Divide the data into two parts , By analogy , Until the whole data is in order .
3 Realize dynamic graph

4 Code implementation
#include <stdio.h>
#include <unistd.h>
int sort(int *num,int low,int high)
{
int flag=num[low];
while(low<high){
while(num[high]>flag&&low<high){
high--;
}
if(low<high){
num[low]=num[high];
}
while(num[low]<flag&&low<high){
low++;
}
if(low<high){
num[high]=num[low];
high--;
}
}
num[low]=flag;
return low;
}
int quick_sort(int *num,int low,int high)
{
if(low<high){
int ret=sort(num,low,high);
quick_sort(num,0,ret-1);
quick_sort(num,ret+1,high);
}
}
int show_num(int *num)
{
for(int i=0;i<10;i++){
printf("%d ",num[i]);
}
puts("");
}
int main(int argc, char const *argv[])
{
int num[10]={3,4,12,65,31,52,34,76,6,10};
show_num(num);
quick_sort(num,0,9);
show_num(num);
return 0;
}
边栏推荐
- 黑马程序员-接口测试-四天学习接口测试-第四天-Postman读取外部数据文件,读取数据文件数据,iHRM项目实战,员工管理模块,添加员工,批量运行测试用例,生成测试报告,
- Tutorial on the principle and application of database system (049) -- MySQL query (XI): sub query
- Chapter 1 water test --*offer
- Introduction to QT (2.1 the first procedure for the beginning of QT)
- 1000个Okaleido Tiger首发上线Binance NFT,引发抢购热潮
- Interviewer: if the order is not paid within 30 minutes after it is generated, it will be automatically cancelled. How to realize it?
- Solve the error: uncaught (in promise) navigationduplicated: avoided redundant navigation to current location:“
- SAP 电商云 Spartacus UI Store 相关的设计明细
- Deep understanding of collaborative process
- [untitled]
猜你喜欢

黑马程序员-接口测试-四天学习接口测试-第四天-Postman读取外部数据文件,读取数据文件数据,iHRM项目实战,员工管理模块,添加员工,批量运行测试用例,生成测试报告,

Sparksql design and introduction, 220722,

Intelligent video monitoring solutions for elderly care institutions, using new technologies to help the intelligent supervision of nursing homes

测试小码农也有大目标,最新BAT大厂面试题大总结(持续更新中...)

Solve the error: uncaught (in promise) navigationduplicated: avoided redundant navigation to current location:“

Centernet target detection model and centerfusion fusion target detection model

Prometheus+node exporter+grafana monitoring server system resources

QT入门篇(2.1初入QT的开始第一个程序)

Starfish OS: create a new paradigm of the meta universe with reality as the link

T-seda code
随机推荐
Network system experiment: solve the problem of Ping failure
Installation and use of appscan
How to relieve the pillow quickly
What is promise? What are the benefits of promise
*offer--2
Introduction to QT (2.1 the first procedure for the beginning of QT)
VLAN division, automatic allocation of IP to all hosts through DHCP, and communication accessible throughout the network
创建自签名证书, 对exe文件进行数字签名
mysql 分支语句case报错
Summary of the fourth week of summer vacation
出于数据安全考虑 荷兰教育部要求学校暂停使用Chrome浏览器
vim常用命令
Intelligent video monitoring solutions for elderly care institutions, using new technologies to help the intelligent supervision of nursing homes
The winverifytrust call returned 80096005 error. The timestamp signature or certificate cannot be verified or is damaged
The way to access global variables in multi-source file mode (extern usage)
Analysis of the advantages of the LAAS scheme of elephant swap led to strong performance of ETOKEN
Redis | very important Middleware
Bean validation custom container validation chapter ----06
Tutorial on principles and applications of database system (047) -- MySQL query (IX): connection query
黑马程序员-接口测试-四天学习接口测试-第四天-Postman读取外部数据文件,读取数据文件数据,iHRM项目实战,员工管理模块,添加员工,批量运行测试用例,生成测试报告,