当前位置:网站首页>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 !
边栏推荐
- Chow Tai Fook fulfills the "centenary commitment" and sincerely serves to promote green environmental protection
- Under the crisis of enterprise development, is digital transformation the future savior of enterprises
- 申请代码签名证书时如何选择合适的证书品牌?
- Postgresql 13 安装
- Microframe technology won the "cloud tripod Award" at the global Cloud Computing Conference!
- 美国费城发生“安全事故” 2名警察遭枪杀
- mysql8.0JSON_CONTAINS的使用说明
- js亮瞎你眼的日期选择器
- There is a powerful and good-looking language bird editor, which is better than typora and developed by Alibaba
- webRTC SDP mslabel lable
猜你喜欢
Penetration testing methodology
可视化任务编排&拖拉拽 | Scaleph 基于 Apache SeaTunnel的数据集成
Two Bi development, more than 3000 reports? How to do it?
Photoshop插件-动作相关概念-ActionList-ActionDescriptor-ActionList-动作执行加载调用删除-PS插件开发
FR练习题目---综合题
想进阿里必须啃透的12道MySQL面试题
PyTorch二分类时BCELoss,CrossEntropyLoss,Sigmoid等的选择和使用
实现一个博客系统----使用模板引擎技术
Implement a blog system -- using template engine technology
Thymeleaf th:classappend attribute append th:styleappend style append th:data- custom attribute
随机推荐
leetcode:881. lifeboat
What about SSL certificate errors? Solutions to common SSL certificate errors in browsers
选择排序和冒泡排序
漫画:优秀的程序员具备哪些属性?
IPv6与IPv4的区别 网信办等三部推进IPv6规模部署
Long list optimized virtual scrolling
微帧科技荣获全球云计算大会“云鼎奖”!
[learning notes] connectivity and circuit of graph
手写promise与async await
Thymeleaf 使用后台自定义工具类处理文本
Chow Tai Fook fulfills the "centenary commitment" and sincerely serves to promote green environmental protection
Thymeleaf th:classappend attribute append th:styleappend style append th:data- custom attribute
长列表优化虚拟滚动
如何将电脑复制的内容粘贴进MobaXterm?如何复制粘贴
裁员下的上海
【NVMe2.0b 14-9】NVMe SR-IOV
729. My schedule I: "simulation" & "line segment tree (dynamic open point) &" block + bit operation (bucket Division) "
I want to inquire about how to ensure data consistency when a MySQL transaction updates multiple tables?
I collect multiple Oracle tables at the same time. After collecting for a while, I will report that Oracle's OGA memory is exceeded. Have you encountered it?
Shanghai under layoffs