当前位置:网站首页>选择排序和冒泡排序
选择排序和冒泡排序
2022-07-05 14:30:00 【老鱼37】
首先开始将的是选择排序:
整体实现思想:
完整代码:
void Swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
//直接选择排序
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;
}
}
//找到最小值和最大值的下标了
// 进行交换
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);
}
冒泡排序:
相信大家学习C语言所接触到的第一个排序就是冒泡排序
不多讲了。
那我这里直接就上代码了:
void Swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
//冒泡排序
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);
}
如有错误!多多指教!
边栏推荐
- 分享 12 个最常用的正则表达式,能解决你大部分问题
- Security analysis of Web Architecture
- Interpretation of tiflash source code (IV) | design and implementation analysis of tiflash DDL module
- Sorter evolution of ticdc 6.0 principle
- Solution of commercial supply chain collaboration platform in household appliance industry: lean supply chain system management, boosting enterprise intelligent manufacturing upgrading
- R语言使用ggplot2包的geom_histogram函数可视化直方图(histogram plot)
- 循环不变式
- Qingda KeYue rushes to the science and Innovation Board: the annual revenue is 200million, and it is proposed to raise 750million
- World Environment Day | Chow Tai Fook serves wholeheartedly to promote carbon reduction and environmental protection
- mysql8.0JSON_ Instructions for using contains
猜你喜欢
Topology visual drawing engine
Make the seckill Carnival more leisurely: the database behind the promotion (Part 2)
世界环境日 | 周大福用心服务推动减碳环保
Intelligent supply chain collaboration system solution for daily chemical products industry: digital intelligent SCM supply chain, which is the "acceleration" of enterprise transformation
How to introduce devsecops into enterprises?
实现一个博客系统----使用模板引擎技术
World Environment Day | Chow Tai Fook serves wholeheartedly to promote carbon reduction and environmental protection
【NVMe2.0b 14-9】NVMe SR-IOV
Penetration testing methodology
Principle and performance analysis of lepton lossless compression
随机推荐
Topology visual drawing engine
Security analysis of Web Architecture
Why do mechanical engineers I know complain about low wages?
做自媒體視頻二次剪輯,怎樣剪輯不算侵權
How to call the function mode of one hand and one machine
Thymeleaf th:classappend属性追加 th:styleappend样式追加 th:data-自定义属性
The speed monitoring chip based on Bernoulli principle can be used for natural gas pipeline leakage detection
Thymeleaf 常用函数
MySQL user-defined function ID number to age (supports 15 / 18 digit ID card)
freesurfer运行完recon-all怎么快速查看有没有报错?——核心命令tail重定向
循环不变式
不相交集
Thymeleaf th:with局部变量的使用
R语言ggplot2可视化密度图:按照分组可视化密度图、自定义配置geom_density函数中的alpha参数设置图像透明度(防止多条密度曲线互相遮挡)
Postgresql 13 安装
Matrix chain multiplication dynamic programming example
Is the securities account given by the head teacher of qiniu school safe? Can I open an account?
实现一个博客系统----使用模板引擎技术
2022年国内正规的期货公司平台有哪些啊?方正中期怎么样?安全可靠吗?
R語言ggplot2可視化:可視化折線圖、使用theme函數中的legend.position參數自定義圖例的比特置