当前位置:网站首页>选择排序和冒泡排序
选择排序和冒泡排序
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);
}
如有错误!多多指教!
边栏推荐
- R語言ggplot2可視化:可視化折線圖、使用theme函數中的legend.position參數自定義圖例的比特置
- Intelligent supply chain collaboration system solution for daily chemical products industry: digital intelligent SCM supply chain, which is the "acceleration" of enterprise transformation
- R language uses boxplot function in native package (basic import package, graphics) to visualize box plot
- 实现一个博客系统----使用模板引擎技术
- [learning notes] connectivity and circuit of graph
- 01. Solr7.3.1 deployment and configuration of jetty under win10 platform
- What are the advantages and characteristics of SAS interface
- Is it OK to open the securities account on the excavation finance? Is it safe?
- LeetCode_ 2 (add two numbers)
- [learning notes] stage test 1
猜你喜欢
Make the seckill Carnival more leisurely: the database behind the promotion (Part 2)
日化用品行业智能供应链协同系统解决方案:数智化SCM供应链,为企业转型“加速度”
Penetration testing methodology
Section - left closed right open
APR protocol and defense
Thymeleaf th:with use of local variables
ASP. Net large takeout ordering system source code (PC version + mobile version + merchant version)
Thymeleaf 使用后台自定义工具类处理文本
Countermeasures of enterprise supply chain management system in UCA Era
Share 20 strange JS expressions and see how many correct answers you can get
随机推荐
How to deeply understand the design idea of "finite state machine"?
Geom of R language using ggplot2 package_ Histogram function visual histogram (histogram plot)
R语言使用ggplot2包的geom_histogram函数可视化直方图(histogram plot)
非技术部门,如何参与 DevOps?
What about SSL certificate errors? Solutions to common SSL certificate errors in browsers
Thymeleaf 使用后台自定义工具类处理文本
[summary of leetcode weekly competition] the 81st fortnight competition of leetcode (6.25)
日化用品行业智能供应链协同系统解决方案:数智化SCM供应链,为企业转型“加速度”
微帧科技荣获全球云计算大会“云鼎奖”!
注意!软件供应链安全挑战持续升级
最长公共子序列 - 动态规划
R语言使用nnet包的multinom函数构建无序多分类logistic回归模型、使用coef函数获取模型中每个变量(自变量改变一个单位)对应的对数优势比(log odds ratio)
Introduction, installation, introduction and detailed introduction to postman!
【NVMe2.0b 14-9】NVMe SR-IOV
mysql8.0JSON_ Instructions for using contains
R语言使用MASS包的polr函数构建有序多分类logistic回归模型、使用coef函数获取模型中每个变量(自变量改变一个单位)对应的对数优势比(log odds ratio)
Thymeleaf 模板的创建与使用
Strong connection component
Google eventbus usage details
04_ Use of solrj7.3 of solr7.3