当前位置:网站首页>[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 .
边栏推荐
- Matplotlib simple logistic regression visualization
- 买股票通过客户经理的开户二维码开户资金是否安全?想开户炒股
- RMB 3000 | record "tbtools" video, make a friend and get a cash prize!
- 三、自动终止训练
- Format analysis and explanation of wav file
- 【MYSQL】索引的理解和使用
- Voiceprint Technology (VI): other applications of voiceprint Technology
- C program termination problem clr20r3 solution
- 106. 简易聊天室9:使用 Socket 传递音频
- JMeter interface test, associated interface implementation steps (token)
猜你喜欢

关掉一个线程

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

2、 Training fashion_ MNIST dataset

Where are the hotel enterprises that have been under pressure since the industry has warmed up in spring?

A 35 year old Tencent employee was laid off and sighed: a suite in Beijing, with a deposit of more than 7 million, was anxious about unemployment

5、 Project practice --- identifying man and horse

对常用I/O模型进行比较说明

C program termination problem clr20r3 solution

自定义注解之编译时注解(RetentionPolicy.CLASS)

【期末复习笔记】数字逻辑
随机推荐
Socket programming -- poll model
Chinese solution cannot be entered after webgl is published
声纹技术(七):声纹技术的未来
使用Navicat对比多环境数据库数据差异和结构差异,以及自动DML和DDL脚本
《JVM》对象内存分配的TLAB机制与G1中的TLAB流程
Voiceprint Technology (III): voiceprint recognition technology
matplotlib matplotlib中axvline()和axhline()函数
通过客户经理的开户二维码开股票账户安全吗?
Prepare for the 1000 Android interview questions and answers that golden nine silver ten must ask in 2022, and completely solve the interview problems
annotation lib 和 processor lib如何引用
首期Techo Day腾讯技术开放日,628等你!
Unity--Configurable Joint——简单教程,带你入门可配置关节
Voiceprint Technology (I): the past and present life of voiceprint Technology
声纹技术(四):声纹识别的工程部署
三、自动终止训练
《乔布斯传》英文原著重点词汇笔记(六)【 chapter three 】
cazy長安戰役八卦迷宮
Stimulsoft ultimate presents reports and dashboards
matplotlib matplotlib中决策边界绘制函数plot_decision_boundary和plt.contourf函数详解
C language: find all integers that can divide y and are odd numbers, and put them in the array indicated by B in the order from small to large