当前位置:网站首页>[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 .
边栏推荐
- 【期末复习笔记】数字逻辑
- IC研发常用英文术语缩写
- Is it safe for Huatai Securities to open a stock account on it?
- Explanation of assertions in JMeter
- Socket programming -- epoll model
- 《乔布斯传》英文原著重点词汇笔记(一)【 Introduction 】
- 声纹技术(一):声纹技术的前世今生
- C#启动程序传递参数丢失双引号,如何解决?
- On the underlying index principle of MySQL
- Oracle one line function Encyclopedia
猜你喜欢
Mapping mode of cache
【无标题】**数据库课设:三天完成学生信息管理系统**
2、 Training fashion_ MNIST dataset
Analysis of a video website m3u8 non perceptual encryption
Matplotlib plt Axis() usage
Where are the hotel enterprises that have been under pressure since the industry has warmed up in spring?
Compile time annotations for custom annotations (retentionpolicy.class)
C language: bubble sort
备战2022年金九银十必问的1000道Android面试题及答案整理,彻底解决面试的烦恼
Explanation of assertions in JMeter
随机推荐
Nodejs using the express framework demo
C#程序终止问题CLR20R3解决方法
2、 Training fashion_ MNIST dataset
How can games copied from other people's libraries be displayed in their own libraries
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
In Section 5 of bramble pie project practice, Nokia 5110 LCD is used to display Hello World
对常用I/O模型进行比较说明
Oracle one line function Encyclopedia
[untitled] * * database course design: complete the student information management system in three days**
【OpenCV】—输入输出XML和YAML文件
Voiceprint Technology (III): voiceprint recognition technology
¥3000 | 录「TBtools」视频,交个朋友&拿现金奖!
On the underlying index principle of MySQL
Atguigu---01-scaffold
声纹技术(二):音频信号处理基础
第十五周作业
声纹技术(六):声纹技术的其他应用
《乔布斯传》英文原著重点词汇笔记(一)【 Introduction 】
3大问题!Redis缓存异常及处理方案总结
Analysis on the bottom calling process of micro service calling component ribbon