当前位置:网站首页>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 !
边栏推荐
- 在Pytorch中使用Tensorboard可视化训练过程
- 729. 我的日程安排表 I :「模拟」&「线段树(动态开点)」&「分块 + 位运算(分桶)」
- 【leetcode周赛总结】LeetCode第 81 场双周赛(6.25)
- Niuke: intercepting missiles
- 【jvm】运算指令
- 直播预告|如何借助自动化工具落地DevOps(文末福利)
- 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?
- [12 classic written questions of array and advanced pointer] these questions meet all your illusions about array and pointer, come on!
- Principle and performance analysis of lepton lossless compression
- 【学习笔记】阶段测试1
猜你喜欢

Principle and performance analysis of lepton lossless compression

Thymeleaf th:with use of local variables

Thymeleaf th:classappend attribute append th:styleappend style append th:data- custom attribute

微帧科技荣获全球云计算大会“云鼎奖”!
![[detailed explanation of Huawei machine test] character statistics and rearrangement](/img/0f/972cde8c749e7b53159c9d9975c9f5.png)
[detailed explanation of Huawei machine test] character statistics and rearrangement

【华为机试真题详解】字符统计及重排

安装配置Jenkins
![[learning notes] stage test 1](/img/22/ad16375d8d1510c2ec75c56403a8bf.png)
[learning notes] stage test 1

周大福践行「百周年承诺」,真诚服务推动绿色环保

日化用品行业智能供应链协同系统解决方案:数智化SCM供应链,为企业转型“加速度”
随机推荐
实现一个博客系统----使用模板引擎技术
在Pytorch中使用Tensorboard可视化训练过程
Jmeter性能测试:ServerAgent资源监控
超级哇塞的快排,你值得学会!
快消品行业SaaS多租户解决方案,构建全产业链数字化营销竞争力
Thymeleaf common functions
Does maxcompute have SQL that can query the current storage capacity (KB) of the table?
微帧科技荣获全球云计算大会“云鼎奖”!
Talking about how dataset and dataloader call when loading data__ getitem__ () function
日化用品行业智能供应链协同系统解决方案:数智化SCM供应链,为企业转型“加速度”
FR练习题目---综合题
机器学习笔记 - 灰狼优化
APR protocol and defense
想进阿里必须啃透的12道MySQL面试题
Using tensorboard to visualize the training process in pytoch
Thymeleaf th:classappend attribute append th:styleappend style append th:data- custom attribute
How to make a second clip of our media video without infringement
面试突击62:group by 有哪些注意事项?
FR练习题目---简单题
【招聘岗位】基础设施软件开发人员