当前位置:网站首页>选择排序和冒泡排序
选择排序和冒泡排序
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);
}
如有错误!多多指教!
边栏推荐
- Assembly language
- Postman简介、安装、入门使用方法详细攻略!
- 别不服气。手机功能升级就是强
- Tdengine biweekly selection of community issues | phase III
- openGauss数据库源码解析系列文章—— 密态等值查询技术详解(下)
- Principle and performance analysis of lepton lossless compression
- Don't be unconvinced. Mobile phone function upgrade is strong
- The speed monitoring chip based on Bernoulli principle can be used for natural gas pipeline leakage detection
- 总量分析 核算方法和势方法 - 分摊分析
- 魅族新任董事長沈子瑜:創始人黃章先生將作為魅族科技產品戰略顧問
猜你喜欢

Tdengine biweekly selection of community issues | phase III

Thymeleaf 模板的创建与使用

ASP. Net large takeout ordering system source code (PC version + mobile version + merchant version)

How to protect user privacy without password authentication?

Section - left closed right open

Countermeasures of enterprise supply chain management system in UCA Era

World Environment Day | Chow Tai Fook serves wholeheartedly to promote carbon reduction and environmental protection

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

循环不变式

Postman简介、安装、入门使用方法详细攻略!
随机推荐
Interpretation of tiflash source code (IV) | design and implementation analysis of tiflash DDL module
Countermeasures of enterprise supply chain management system in UCA Era
SaaS multi tenant solution for FMCG industry to build digital marketing competitiveness of the whole industry chain
分享 12 个最常用的正则表达式,能解决你大部分问题
家用电器行业商业供应链协同平台解决方案:供应链系统管理精益化,助推企业智造升级
Postman简介、安装、入门使用方法详细攻略!
展现强大。这样手机就不会难前进
World Environment Day | Chow Tai Fook serves wholeheartedly to promote carbon reduction and environmental protection
微帧科技荣获全球云计算大会“云鼎奖”!
SSL证书错误怎么办?浏览器常见SSL证书报错解决办法
做自媒体视频二次剪辑,怎样剪辑不算侵权
Sharing the 12 most commonly used regular expressions can solve most of your problems
Is the securities account given by the head teacher of qiniu school safe? Can I open an account?
PHP - fatal error: allowed memory size of 314572800 bytes exhausted
Judge whether the variable is an array
How to call the function mode of one hand and one machine
Discussion on memset assignment
What are the advantages and characteristics of SAS interface
Topology visual drawing engine
The function of qualifier in C language