当前位置:网站首页>Insert sort directly
Insert sort directly
2022-07-27 15:45:00 【FA FA is a silly goose】
1. Direct insert sort
Insert the elements to be sorted in ascending or descending order into the appropriate position .
2. principle
Take ascending order , Start with the second element of the array , from a[0] Start comparing , If you know to find the right place , That is to ensure that it is smaller than the following elements .
3. Code implementation
#include <stdio.h>
// Direct insert sort
void display(int arr[],int size)
{
for(int i=0;i<size;i++)
printf("%d ",arr[i]);
printf("\n");
}
void InsertSort(int arr[],int size) // Take ascending order
{
int i,j;
for(i=1;i<size;i++)
{
if(arr[i-1]>arr[i])
{
int temp=arr[i];
for(j=i-1;j>=0 && arr[j]>temp;j--)
arr[j+1]=arr[j];
arr[j+1]=temp;
}
}
}
int main()
{
int arr[]={
9,18,27,6,25,44,3,12,1};
int size=sizeof(arr)/sizeof(arr[0]);
printf(" The array size is :%d\n",size);
display(arr,size); // Before ordering
InsertSort(arr,size);
display(arr,size); // After ordering
return 0;
}
4. Running results 
边栏推荐
- Network equipment hard core technology insider router Chapter 17 dpdk and its prequel (II)
- 传美国政府将向部分美企发放对华为销售许可证!
- 使用解构交换两个变量的值
- 复杂度分析
- Network device hard core technology insider router Chapter 15 from deer by device to router (Part 2)
- JS uses unary operators to simplify string to number conversion
- C语言:字符串函数与内存函数
- Voice live broadcast system -- a necessary means to improve the security of cloud storage
- Go language learning notes (1)
- Spark lazy list files 的实现
猜你喜欢
随机推荐
[正则表达式] 匹配开头和结尾
Leetcode 783. binary search tree node minimum distance tree /easy
Zhaoqi scientific innovation and entrepreneurship competition planning and undertaking organization, mass entrepreneurship and innovation platform, project landing and docking
Spark Bucket Table Join
复杂度分析
JS uses for in and for of to simplify ordinary for loops
js使用for in和for of来简化普通for循环
【剑指offer】面试题50:第一个只出现一次的字符——哈希表查找
It is said that the US government will issue sales licenses to Huawei to some US enterprises!
$router.back(-1)
/dev/loop1占用100%问题
Pytorch replaces some components in numpy. / / please indicate the source of the reprint
Spark RPC
Learn parquet file format
Fluent -- layout principle and constraints
Spark Filter算子在Parquet文件上的下推
【云享读书会第13期】音频文件的封装格式和编码格式
C语言:动态内存函数
Complexity analysis
C语言:字符串函数与内存函数








![[TensorBoard] OSError: [Errno 22] Invalid argument处理](/img/bf/c995f487607e3b307a268779ec1e94.png)
