当前位置:网站首页>选择排序和插入排序
选择排序和插入排序
2022-07-02 06:30:00 【代码海贼团船长】
1.选择排序
1)按从大到小选择排序,从第0个数开始;
2)寻找最大值的角标max,对应的数组中的值a[max];
3)把获得的a[max]与a[0]交换;
4)从第1个数开始,重复2)3);
5)结束。
void selectMax(int a[], int n)
{
int max;
int temp;
for (int j = 0; j < n - 1; j++)
{
max = j;
for (int i = j + 1; i < n; i++)
{
if (a[max] < a[i])
{
max = i;//遍历,获取数组中最大值的角标
}
}
if (max != j) //将最大值放到第一位
{
temp = a[max];
a[max] = a[j];
a[j] = temp;
}
}
}
2.插入排序
void InsertSort(int a[], int len)
{
int temp, i, j;
for (i = 1; i < len; i++)
{
if (a[i] < a[i - 1])
{
temp = a[i]; //用一个临时变量存一下
for (j = i - 1; a[j] > temp&& j >= 0; j--)
{
a[j + 1] = a[j]; //凡事比i这个数大的就要后移,因为大的数总是在后面
}
a[j + 1] = temp; //这里需要注意的是j+1,调bug好累勒 ->@@
}
}
}
边栏推荐
- k8s入门:Helm 构建 MySQL
- [untitled]
- Use the kaggle training model and download your own training model
- What is SQL injection
- Sqli labs Level 2
- Tensorflow2 keras 分类模型
- [blackmail virus data recovery] suffix Crylock blackmail virus
- Use Wireshark to grab TCP three handshakes
- Valin cable: BI application promotes enterprise digital transformation
- Minecraft空岛服开服
猜你喜欢
Flex layout
Tcp/ip - transport layer
使用wireshark抓取Tcp三次握手
双向链表的实现(双向链表与单向链表的简单区别联系和实现)
Illegal use of crawlers, an Internet company was terminated, the police came to the door, and 23 people were taken away
Solid principle: explanation and examples
Service de groupe minecraft
Realization of basic function of sequence table
OpenFeign 简单使用
Web security -- Logical ultra vires
随机推荐
双向链表的实现(双向链表与单向链表的简单区别联系和实现)
Nacos download, start and configure MySQL database
Minecraft安装资源包
Gateway 简单使用
c语言将字符串中的空格替换成%20
Call Stack
Benefits of ufcs of D
OpenShift构建镜像
HCIA—数据链路层
Illegal use of crawlers, an Internet company was terminated, the police came to the door, and 23 people were taken away
Rotating linked list (illustration)
实现双向链表(带傀儡节点)
Realize bidirectional linked list (with puppet node)
判断是否是数独
程序猿学英语-指令式编程
gocv边界填充
Minecraft空岛服开服
Routing foundation - dynamic routing
k8s入门:Helm 构建 MySQL
Linux安装Oracle Database 19c RAC