当前位置:网站首页>[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 .
边栏推荐
- [opencv] - input and output XML and yaml files
- JMeter interface test, associated interface implementation steps (token)
- Mapping mode of cache
- jmeter中csv参数化
- How to become a software testing expert? From 3K to 17k a month, what have I done?
- matplotlib matplotlib中决策边界绘制函数plot_decision_boundary和plt.contourf函数详解
- Notes on key words in the original English work biography of jobs (III) [chapter one]
- Oracle one line function Encyclopedia
- [MySQL] understanding and use of indexes
- sklearn 高维数据集制作make_circles 和 make_moons
猜你喜欢

Oracle-单行函数大全

matplotlib matplotlib中plt.axis()用法

C language: count the number of words in a paragraph

5、 Project practice --- identifying man and horse

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

Atguigu---17-life cycle

A game WP

matplotlib matplotlib中axvline()和axhline()函数

备战2022年金九银十必问的1000道Android面试题及答案整理,彻底解决面试的烦恼

(translation) the use of letter spacing to improve the readability of all capital text
随机推荐
【MYSQL】索引的理解和使用
cazy長安戰役八卦迷宮
Oracle-单行函数大全
Format analysis and explanation of wav file
【OpenCV】—输入输出XML和YAML文件
C language: bubble sort
The first techo day Tencent technology open day, 628 waiting for you!
5、 Project practice --- identifying man and horse
Summary of hardfault problem in RTOS multithreading
Are the top ten securities companies at great risk of opening accounts and safe and reliable?
一、单个神经元网络构建
[MySQL] understanding and use of indexes
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
neo4jDesktop(neo4j桌面版)配置自动启动(开机自启)
How annotation lib and processor lib reference
Compile time annotations for custom annotations (retentionpolicy.class)
4、 Convolution neural networks
三、自动终止训练
Atguigu---01-scaffold
3大问题!Redis缓存异常及处理方案总结