当前位置:网站首页>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 !
边栏推荐
- 【招聘岗位】软件工程师(全栈)- 公共安全方向
- 注意!软件供应链安全挑战持续升级
- Jmeter性能测试:ServerAgent资源监控
- 【华为机试真题详解】字符统计及重排
- 实现一个博客系统----使用模板引擎技术
- Two Bi development, more than 3000 reports? How to do it?
- What about SSL certificate errors? Solutions to common SSL certificate errors in browsers
- 【NVMe2.0b 14-9】NVMe SR-IOV
- GPS原始坐标转百度地图坐标(纯C代码)
- MongDB学习笔记
猜你喜欢

Countermeasures of enterprise supply chain management system in UCA Era

MongDB学习笔记

Online electronic component purchasing Mall: break the problem of information asymmetry in the purchasing process, and enable enterprises to effectively coordinate management

APR protocol and defense

PyTorch二分类时BCELoss,CrossEntropyLoss,Sigmoid等的选择和使用

Two Bi development, more than 3000 reports? How to do it?

【数组和进阶指针经典笔试题12道】这些题,满足你对数组和指针的所有幻想,come on !

如何将电脑复制的内容粘贴进MobaXterm?如何复制粘贴

【NVMe2.0b 14-9】NVMe SR-IOV

Niuke: intercepting missiles
随机推荐
js亮瞎你眼的日期选择器
PyTorch二分类时BCELoss,CrossEntropyLoss,Sigmoid等的选择和使用
开挖财上的证券账户可以吗?安全吗?
Selection and use of bceloss, crossentropyloss, sigmoid, etc. in pytorch classification
一键更改多个文件名字
Interview shock 62: what are the precautions for group by?
注意!软件供应链安全挑战持续升级
How to solve the problem of garbled code when installing dependency through NPM or yarn
Stm32+bh1750 photosensitive sensor obtains light intensity
有一个强大又好看的,赛过Typora,阿里开发的语雀编辑器
APR protocol and defense
详解Vue适时清理keepalive缓存方案
Thymeleaf 模板的创建与使用
There is a powerful and good-looking language bird editor, which is better than typora and developed by Alibaba
How to protect user privacy without password authentication?
Countermeasures of enterprise supply chain management system in UCA Era
机器学习笔记 - 灰狼优化
I want to inquire about how to ensure data consistency when a MySQL transaction updates multiple tables?
机器学习框架简述
How to open an account of qiniu securities? Is it safe to open an account?