当前位置:网站首页>Select sort and insert sort
Select sort and insert sort
2022-07-02 08:48:00 【Code pirate captain】
1. Selection sort
1) Sort by selection from large to small , From 0 The number begins ;
2) Find the angle sign of the maximum value max, The value in the corresponding array a[max];
3) Take what you get a[max] And a[0] In exchange for ;
4) From 1 The number begins , repeat 2)3);
5) end .
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;// Traverse , Get the subscript of the maximum value in the array
}
}
if (max != j) // Put the maximum value first
{
temp = a[max];
a[max] = a[j];
a[j] = temp;
}
}
}
2. Insertion sort
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]; // Save it with a temporary variable
for (j = i - 1; a[j] > temp&& j >= 0; j--)
{
a[j + 1] = a[j]; // Compare everything i The big one will move back , Because big numbers are always behind
}
a[j + 1] = temp; // What needs to be noted here is j+1, transfer bug So tired ->@@
}
}
}
边栏推荐
- Minecraft空岛服开服
- Zipkin is easy to use
- Web security -- core defense mechanism
- History of Web Technology
- Qunhui NAS configuring iSCSI storage
- Web security -- Logical ultra vires
- 整理秒杀系统的面试必备!!!
- sqli-labs(POST类型注入)
- Realization of basic function of sequence table
- Use the numbers 5, 5, 5, 1 to perform four operations. Each number should be used only once, and the operation result value is required to be 24
猜你喜欢

Routing foundation - dynamic routing

Web security -- core defense mechanism

Linux安装Oracle Database 19c RAC

TCP/IP—传输层

OpenShift构建镜像

Linked list classic interview questions (reverse the linked list, middle node, penultimate node, merge and split the linked list, and delete duplicate nodes)

HCIA—数据链路层

c语言自定义类型枚举,联合(枚举的巧妙使用,联合体大小的计算)

Sentinel easy to use

STM32 new project (refer to punctual atom)
随机推荐
k8s入门:Helm 构建 MySQL
路由基础—动态路由
Web安全--核心防御机制
Data asset management function
sqli-labs第8关(布尔盲注)
Qunhui NAS configuring iSCSI storage
idea中注释代码取消代码的快捷键
寻找链表中值域最小的节点并移到链表的最前面
Minecraft模组服开服
小米电视不能访问电脑共享文件的解决方案
File upload Labs
Flex layout
Gateway 简单使用
OpenFeign 簡單使用
HCIA—应用层
Web security -- core defense mechanism
Getting started with k8s: building MySQL with Helm
Gateway is easy to use
Aneng logistics' share price hit a new low: the market value evaporated by nearly 10 billion yuan, and it's useless for chairman Wang Yongjun to increase his holdings
统计字符串中各类字符的个数