当前位置:网站首页>[learn C from me and master the key to programming] insertion sort of eight sorts
[learn C from me and master the key to programming] insertion sort of eight sorts
2022-06-25 09:07:00 【zbossz】
1. Insertion sort

Purple is the subscript .
I drew a picture for you , Insert , It's like playing cards , A novice may start by simply taking a hand , Then insert one by one . The same is true for insert sorting , We have to start at a specific location , Compare with the previous position in turn , Then we can sort according to our ideas , The graph above is in ascending order .
We probably understand the meaning , What about us , Think about it Code :
First, we need to write the insertion logic :
void InsertSort(int* arr, int n)
{
int end = 0;
int temp = arr[end + 1];
if (arr[end] > temp)
{
arr[end + 1] = arr[end];
arr[end] = temp;
}
else
{
;
}
}
The above is a relatively simple logic , We need to control the loop that is in the range of an array , To meet our requirements .
void InsertSort(int* arr, int n)
{
int end = 0;
while (end >= 0)
{
int temp = arr[end + 1];
if (arr[end] > temp)
{
arr[end + 1] = arr[end];
arr[end] = temp;
}
else
{
;
}
end--;
}
}
Some people may feel it's over when they get here , Let's run , See if it's over ?
What's going on? ?
Why is it still out of order ?
We found that the above code can only insert end+1 The previous subscript , Make it orderly , So we have to set a quantity , send end It can change .
void InsertSort(int* arr, int n)
{
for (int i = 0;i < n - 1;i++)
{
int end = i;
while (end >= 0)
{
int temp = arr[end + 1];
if (arr[end] > temp)
{
arr[end + 1] = arr[end];
arr[end] = temp;
}
else
{
;
}
end--;
}
}
}
int main()
{
int arr[] = {
9,8,7,6,5,4,3,2,1 };
InsertSort(arr, sizeof(arr) / sizeof(arr[0]));
for (int i = 0;i < sizeof(arr) / sizeof(arr[0]);i++)
{
printf("%d ", arr[i]);
}
return 0;
}
result :
This is c Language to achieve insertion sorting .
边栏推荐
- compiling stm32f4xx_it.c... “.\Objects\BH-F407.axf“ - 42 Error(s), 1 Warning(s).
- 【期末复习笔记】数字逻辑
- LVS-DR模式单网段案例
- compiling stm32f4xx_ it. c... “.\Objects\BH-F407.axf“ - 42 Error(s), 1 Warning(s).
- 4、 Convolution neural networks
- Unity发布webGL的时候JsonConvert.SerializeObject()转换失败
- 《乔布斯传》英文原著重点词汇笔记(三)【 chapter one】
- C language: bubble sort
- Jmeter中的断言使用讲解
- Compile time annotations for custom annotations (retentionpolicy.class)
猜你喜欢

Is the client that gets the scanning code wechat or Alipay

Unity发布webGL的时候JsonConvert.SerializeObject()转换失败

In Section 5 of bramble pie project practice, Nokia 5110 LCD is used to display Hello World

The first techo day Tencent technology open day, 628 waiting for you!

Matplotlib simple logistic regression visualization

Atguigu---17-life cycle

【无标题】**数据库课设:三天完成学生信息管理系统**

C program termination problem clr20r3 solution

Explanation of assertions in JMeter

cazy長安戰役八卦迷宮
随机推荐
5、 Project practice --- identifying man and horse
关掉一个线程
Object. Can defineproperty also listen for array changes?
SQL高级
compiling stm32f4xx_ it. c... “.\Objects\BH-F407.axf“ - 42 Error(s), 1 Warning(s).
How can games copied from other people's libraries be displayed in their own libraries
C # startup program loses double quotation marks for parameters passed. How to solve it?
[MySQL] understanding and use of indexes
通过客户经理的开户二维码开股票账户安全吗?还是去证券公司开户安全?
Flitter gets the height of the top status bar
自定义注解之编译时注解(RetentionPolicy.CLASS)
Matplotlib plt Axis() usage
Analysis on the bottom calling process of micro service calling component ribbon
A game WP
atguigu----01-脚手架
Unknown table 'column of MySQL_ STATISTICS‘ in information_ schema (1109)
Is it safe for Huatai Securities to open a stock account on it?
1、 Construction of single neural network
CSV parameterization in JMeter
Socket programming -- epoll model