当前位置:网站首页>选择排序和插入排序
选择排序和插入排序
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好累勒 ->@@
}
}
}
边栏推荐
- Programmer training, crazy job hunting, overtime ridiculed by colleagues deserve it
- Routing foundation - dynamic routing
- Loadbalancer dynamically refreshes Nacos server
- Web安全--核心防御机制
- cve_ 2019_ 0708_ bluekeep_ Rce vulnerability recurrence
- Benefits of ufcs of D
- Honeypot attack and defense drill landing application scheme
- Classes and objects (instantiation of classes and classes, this, static keyword, encapsulation)
- Sqli labs level 12
- 路由基础—动态路由
猜你喜欢
Minecraft群组服开服
2022 Heilongjiang's latest eight member (Safety Officer) simulated test question bank and answers
Luogu greedy part of the backpack line segment covers the queue to receive water
旋转链表(图解说明)
Generate database documents with one click, which can be called swagger in the database industry
Tcp/ip - transport layer
Openfeign is easy to use
Use the kaggle training model and download your own training model
整理秒杀系统的面试必备!!!
Minecraft空岛服开服
随机推荐
Flex layout
OpenFeign 簡單使用
The source code of the live app. When the verification method is mailbox verification, the verification code is automatically sent to the entered mailbox
gocv图片读取并展示
路由基础—动态路由
Pointer initialization
How to build the alliance chain? How much is the development of the alliance chain
CarSim learning experience - rough translation 1
Sentinel easy to use
Jz-061-serialized binary tree
Realize bidirectional linked list (with puppet node)
Illegal use of crawlers, an Internet company was terminated, the police came to the door, and 23 people were taken away
Learning C
Don't know mock test yet? An article to familiarize you with mock
Live broadcast platform development, flexible menu, and freely adjust the horizontal size of the menu bar
Gateway is easy to use
OpenShift构建镜像
Dip1000 implicitly tagged with fields
Minecraft安装资源包
Honeypot attack and defense drill landing application scheme