当前位置:网站首页>选择排序和冒泡排序
选择排序和冒泡排序
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);
}
如有错误!多多指教!
边栏推荐
- Section - left closed right open
- Thymeleaf th:classappend属性追加 th:styleappend样式追加 th:data-自定义属性
- 分享 20 个稀奇古怪的 JS 表达式,看看你能答对多少
- Why do mechanical engineers I know complain about low wages?
- Qingda KeYue rushes to the science and Innovation Board: the annual revenue is 200million, and it is proposed to raise 750million
- What about SSL certificate errors? Solutions to common SSL certificate errors in browsers
- Disjoint Set
- [learning notes] connectivity and circuit of graph
- 怎么叫一手一机的功能方式
- Webrtc learning (II)
猜你喜欢

Thymeleaf 使用后台自定义工具类处理文本

申请代码签名证书时如何选择合适的证书品牌?

Pointer operation - C language

Sorter evolution of ticdc 6.0 principle

Lepton 无损压缩原理及性能分析
![[summary of leetcode weekly competition] the 81st fortnight competition of leetcode (6.25)](/img/d7/f49bca8da2ce286c18508325985990.png)
[summary of leetcode weekly competition] the 81st fortnight competition of leetcode (6.25)

Tidb DM alarm DM_ sync_ process_ exists_ with_ Error troubleshooting

Shen Ziyu, nouveau Président de Meizu: M. Huang Zhang, fondateur de Meizu, agira comme conseiller stratégique pour les produits scientifiques et technologiques de Meizu

Topology visual drawing engine

微帧科技荣获全球云计算大会“云鼎奖”!
随机推荐
Total amount analysis accounting method and potential method - allocation analysis
Don't be unconvinced. Mobile phone function upgrade is strong
【leetcode周赛总结】LeetCode第 81 场双周赛(6.25)
How can non-technical departments participate in Devops?
一网打尽异步神器CompletableFuture
Strong connection component
日化用品行业智能供应链协同系统解决方案:数智化SCM供应链,为企业转型“加速度”
PHP - fatal error: allowed memory size of 314572800 bytes exhausted
01. Solr7.3.1 deployment and configuration of jetty under win10 platform
Sharing the 12 most commonly used regular expressions can solve most of your problems
【学习笔记】阶段测试1
How to protect user privacy without password authentication?
Make the seckill Carnival more leisurely: the database behind the promotion (Part 2)
Mysql database installation tutorial under Linux
The speed monitoring chip based on Bernoulli principle can be used for natural gas pipeline leakage detection
R language ggplot2 visual density map: Visual density map by group and custom configuration geom_ The alpha parameter in the density function sets the image transparency (to prevent multiple density c
申请代码签名证书时如何选择合适的证书品牌?
Interpretation of tiflash source code (IV) | design and implementation analysis of tiflash DDL module
CyCa children's physical etiquette Ningbo training results assessment came to a successful conclusion
Thymeleaf common functions