当前位置:网站首页>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 !
边栏推荐
- [detailed explanation of Huawei machine test] happy weekend
- 实现一个博客系统----使用模板引擎技术
- [summary of leetcode weekly competition] the 81st fortnight competition of leetcode (6.25)
- 机器学习框架简述
- 可视化任务编排&拖拉拽 | Scaleph 基于 Apache SeaTunnel的数据集成
- Thymeleaf 常用函数
- Photoshop plug-in - action related concepts - actions in non loaded execution action files - PS plug-in development
- be careful! Software supply chain security challenges continue to escalate
- FR练习题目---简单题
- [detailed explanation of Huawei machine test] character statistics and rearrangement
猜你喜欢

IPv6与IPv4的区别 网信办等三部推进IPv6规模部署

CPU设计相关笔记

Loop invariant

【jvm】运算指令

FR练习题目---综合题

Solution of commercial supply chain collaboration platform in household appliance industry: lean supply chain system management, boosting enterprise intelligent manufacturing upgrading

729. My schedule I: "simulation" & "line segment tree (dynamic open point) &" block + bit operation (bucket Division) "

实现一个博客系统----使用模板引擎技术

Section - left closed right open

Niuke: intercepting missiles
随机推荐
手写promise与async await
useMemo,memo,useRef等相关hooks详解
leetcode:881. 救生艇
Microframe technology won the "cloud tripod Award" at the global Cloud Computing Conference!
Implement a blog system -- using template engine technology
How to make a second clip of our media video without infringement
Intelligent supply chain collaboration system solution for daily chemical products industry: digital intelligent SCM supply chain, which is the "acceleration" of enterprise transformation
乌卡时代下,企业供应链管理体系的应对策略
PostgreSQL 13 installation
Fonctions communes de thymeleaf
CPU design practice - Chapter 4 practical task 2 using blocking technology to solve conflicts caused by related problems
Disjoint Set
STM32+BH1750光敏传感器获取光照强度
PHP - fatal error: allowed memory size of 314572800 bytes exhausted
CPU design practice - Chapter 4 practice task 3 use pre delivery technology to solve conflicts caused by related issues
Solution of commercial supply chain collaboration platform in household appliance industry: lean supply chain system management, boosting enterprise intelligent manufacturing upgrading
基于TI DRV10970驱动直流无刷电机
[learning notes] connectivity and circuit of graph
IPv6与IPv4的区别 网信办等三部推进IPv6规模部署
MongDB学习笔记