当前位置:网站首页>Select sort and bubble sort
Select sort and bubble sort
2022-07-05 14:48:00 【Old fish 37】
The first thing to start with is selection sorting :
Overall realization idea :
Complete code :
void Swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
// Direct selection sorting
void SelectSort(int* arr, int len)
{
int begin = 0;
int end = len - 1;
while (begin < end)
{
int mini = begin;
int maxi = begin;
for (int i = begin + 1; i <= end; i++)
{
if (arr[i] < arr[mini])
{
mini = i;
}
if (arr[i] > arr[maxi])
{
maxi = i;
}
}
// Find the subscript of the minimum and maximum
// swapping
Swap(&arr[begin], &arr[mini]);
if (begin ==maxi)
{
maxi = mini;
}
Swap(&arr[end], &arr[maxi]);
--end;
++begin;
}
}
void PrintSort(int* arr, int len)
{
for (int i = 0; i < len; i++)
{
printf("%d ", arr[i]);
}
}
int main()
{
int arr[] = { 3,1,2,4,9,5,6};
int len = sizeof(arr) / sizeof(arr[0]);
SelectSort(arr, len);
PrintSort(arr, len);
}
Bubble sort :
I believe you can learn C The first sort that language comes into contact with is bubble sort
Not much .
Then I'll put the code directly here :
void Swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
// Bubble sort
void BubbleSort(int* arr, int len)
{
for (int i = 0; i < len-1; i++)
{
int flag = 0;
for (int j = 1; j < len - i; j++)
{
if (arr[j - 1] < arr[j])
{
Swap(&arr[j - 1], &arr[j]);
flag = 1;
}
}
if (flag == 0)
{
break;
}
}
}
void PrintSort(int* arr, int len)
{
for (int i = 0; i < len; i++)
{
printf("%d ", arr[i]);
}
}
int main()
{
int arr[] = { 3,1,2,4,9,5,6};
int len = sizeof(arr) / sizeof(arr[0]);
BubbleSort(arr, len);
PrintSort(arr, len);
}
If there is a mistake ! Your smile !
边栏推荐
- 浅谈Dataset和Dataloader在加载数据时如何调用到__getitem__()函数
- 计算中间件 Apache Linkis参数解读
- 【华为机试真题详解】字符统计及重排
- CyCa children's physical etiquette Ningbo training results assessment came to a successful conclusion
- 手写promise与async await
- How to open an account of qiniu securities? Is it safe to open an account?
- Thymeleaf th:with局部变量的使用
- Disjoint Set
- 729. My schedule I: "simulation" & "line segment tree (dynamic open point) &" block + bit operation (bucket Division) "
- Explain Vue's plan to clean up keepalive cache in time
猜你喜欢
可视化任务编排&拖拉拽 | Scaleph 基于 Apache SeaTunnel的数据集成
【华为机试真题详解】字符统计及重排
Photoshop插件-动作相关概念-ActionList-ActionDescriptor-ActionList-动作执行加载调用删除-PS插件开发
Principle and performance analysis of lepton lossless compression
Security analysis of Web Architecture
Thymeleaf th:with use of local variables
Intelligent supply chain collaboration system solution for daily chemical products industry: digital intelligent SCM supply chain, which is the "acceleration" of enterprise transformation
危机重重下的企业发展,数字化转型到底是不是企业未来救星
CPU设计相关笔记
选择排序和冒泡排序
随机推荐
Interview shock 62: what are the precautions for group by?
Microframe technology won the "cloud tripod Award" at the global Cloud Computing Conference!
Shanghai under layoffs
裁员下的上海
Using tensorboard to visualize the training process in pytoch
Photoshop plug-in action related concepts actionlist actiondescriptor actionlist action execution load call delete PS plug-in development
dynamic programming
There is a powerful and good-looking language bird editor, which is better than typora and developed by Alibaba
Photoshop插件-动作相关概念-非加载执行动作文件中动作-PS插件开发
Isn't it right to put money into the external market? How can we ensure safety?
Thymeleaf th:classappend属性追加 th:styleappend样式追加 th:data-自定义属性
申请代码签名证书时如何选择合适的证书品牌?
Topology visual drawing engine
[C question set] of Ⅷ
日化用品行业智能供应链协同系统解决方案:数智化SCM供应链,为企业转型“加速度”
机器学习笔记 - 灰狼优化
Detailed explanation of usememo, memo, useref and other relevant hooks
在Pytorch中使用Tensorboard可视化训练过程
CPU设计实战-第四章实践任务二用阻塞技术解决相关引发的冲突
How can non-technical departments participate in Devops?