当前位置:网站首页>选择排序和冒泡排序
选择排序和冒泡排序
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);
}
如有错误!多多指教!
边栏推荐
- Thymeleaf 常用函数
- 动态规划
- mysql8.0JSON_CONTAINS的使用说明
- 2022年国内正规的期货公司平台有哪些啊?方正中期怎么样?安全可靠吗?
- Faire un clip vidéo auto - média deux fois, comment clip n'est pas considéré comme une infraction
- MySQL user-defined function ID number to age (supports 15 / 18 digit ID card)
- R language ggplot2 visualization: gganimate package is based on Transition_ The time function creates dynamic scatter animation (GIF) and uses shadow_ Mark function adds static scatter diagram as anim
- Matrix chain multiplication dynamic programming example
- 世界环境日 | 周大福用心服务推动减碳环保
- R language uses the multinom function of NNET package to build an unordered multi classification logistic regression model, and uses the coef function to obtain the log odds ratio corresponding to eac
猜你喜欢

How does redis implement multiple zones?

Why do mechanical engineers I know complain about low wages?

循环不变式

How to choose the appropriate certificate brand when applying for code signing certificate?

What are the advantages and characteristics of SAS interface

Tdengine biweekly selection of community issues | phase III

Introduction, installation, introduction and detailed introduction to postman!

Interpretation of tiflash source code (IV) | design and implementation analysis of tiflash DDL module

非技术部门,如何参与 DevOps?

Intelligent supply chain collaboration system solution for daily chemical products industry: digital intelligent SCM supply chain, which is the "acceleration" of enterprise transformation
随机推荐
Interpretation of tiflash source code (IV) | design and implementation analysis of tiflash DDL module
不相交集
【学习笔记】阶段测试1
LeetCode_ 3 (longest substring without repeated characters)
Opengauss database source code analysis series articles -- detailed explanation of dense equivalent query technology (Part 2)
怎么叫一手一机的功能方式
区间 - 左闭右开
Postgresql 13 安装
一网打尽异步神器CompletableFuture
Structure - C language
Fonctions communes de thymeleaf
R Language ggplot2 Visualization: visualize linegraph, using Legend in Theme function. Paramètre de position emplacement de la légende personnalisée
R语言使用MASS包的polr函数构建有序多分类logistic回归模型、使用coef函数获取模型中每个变量(自变量改变一个单位)对应的对数优势比(log odds ratio)
Thymeleaf common functions
Geom of R language using ggplot2 package_ Histogram function visual histogram (histogram plot)
R语言dplyr包select函数、group_by函数、mutate函数、cumsum函数计算dataframe分组数据中指定数值变量的累加值、并生成累加数据列
mysql8.0JSON_CONTAINS的使用说明
【NVMe2.0b 14-9】NVMe SR-IOV
CYCA少儿形体礼仪 宁波市培训成果考核圆满落幕
Thymeleaf 常用函数