当前位置:网站首页>C language implements eight sorts (1)
C language implements eight sorts (1)
2022-06-11 21:57:00 【Code loving students】
1. Insertion sort
What is insert sort ?
Insert sort is the simplest and most intuitive sort algorithm , It works by building an ordered sequence , For unsorted data , Scan backward and forward in sorted series , Locate and insert .
1.1 Simple insert sort
The code implementation is as follows :
void InsertSort(int*a,int length){
int i,j;
for(i=1;i<length;i++){ // from 1 It starts with the subscript 0 There were no elements before
if(a[i]<a[i-1]){
int key=a[i];
j=i-1;
while(j>=0&&key<a[j]){ // Until greater than is found a[j] Number of numbers perhaps j=-1, Insert in first position
a[j+1]=a[j];
j--;
}
a[j+1]=key;
}
}
}
int main(){
int a[]={1,2,7,4,5,9,6};
int length=sizeof(a)/sizeof(a[0]);
InsertSort(a,length);
for(int i=0;i<length;i++){// Print
printf("%d ",a[i]);
}
return 0;
} However, we believe that this insertion method can still be improved , So we created a new insertion method -- Half insertion
Be careful : What we implement here is sorting from small to large !
The code implementation is as follows :
void InsertSort(int*a,int length){
int i,j,left,right;
for(i=1;i<length;i++){ // from 1 It starts with the subscript 0 There were no elements before
int key=a[i];
left=0;
right=i-1;
while(left<=right){
int mid = (left+right)/2;
if(a[mid]>key){
right=mid-1;
}else{
left=mid+1;
}
}
for(j=i-1;j>=right+1;j--){
a[j+1]=a[j];
}
a[right+1]=key;
}
}1.2 Shell Sort
Hill sort is also an insert sort , We find that the closer to the basic order before sorting , The more efficient the insertion sort is , To improve our efficiency , We can sort across multiple times , Make our sorting speed as fast as possible .
The implementation code is as follows :
void ShellSort(int*a,int d,int length){
int i,j;
for(;d>=0;d-=2){// Gradually reduce the stride , Until 1 It becomes a normal insertion sort
for(i=d;i<length;i++){
if(a[i]<a[i-d]){
int key=a[i];
for(j=i-d;j>=0&&key<a[j];j-=d){
a[j+d]=a[j];
}
a[j+d]=key;
}
}
}
}
int main(){
int a[]={1,2,7,4,5,9,6};
int length=sizeof(a)/sizeof(a[0]);
ShellSort(a,5,length);
for(int i=0;i<length;i++){
printf("%d ",a[i]);
}
return 0;
} 边栏推荐
- JVM | virtual machine stack (local variable table; operand stack; dynamic link; method binding mechanism; method call; method return address)
- 行而不辍,未来可期|云扩科技入选上海市专精特新企业
- Leetcode-129- sum of numbers from root node to leaf node
- C语言实现八种排序(2)
- Classes and objects (4)
- Conception du Processeur superscalaire Yao yongbin chapitre 2 cache - - sous - section 2.4 extrait
- 每日一题-删除有序数组的重复项
- Leetcode-104- maximum depth of binary tree
- 效率起飞啊!还能这样开发的?
- Experiment 10 Bezier curve generation - experiment improvement - control point generation of B-spline curve
猜你喜欢

How to use RPA robot to start the first step of digital transformation of freight forwarding industry?
![[niuke.com] ky41 put apples](/img/55/cc246aed1438fdd245530beb7574f0.jpg)
[niuke.com] ky41 put apples

每日一题 - 罗马数字转整数

Why is rpa+ low code a powerful tool to accelerate the digital transformation of finance?

crontab中定时执行shell脚本

「大模型」之所短,「知识图谱」之所长

R language book learning 03 "in simple terms R language data analysis" - Chapter 8 logistic regression model Chapter 9 clustering model

How does the chief financial officer of RPA find the "super entrance" of digital transformation?

华为设备配置HoVPN

R语言书籍学习03 《深入浅出R语言数据分析》-第八章 逻辑回归模型 第九章 聚类模型
随机推荐
[v2.1] automatic update system based on motion step API (repair bug, increase completion display, support disconnection reconnection and data compensation)
Rexroth overflow valve zdb6vp2-42/315v
如何查看win系统的安装日期
Take off efficiently! Can it be developed like this?
R language book learning 03 "in simple terms R language data analysis" - Chapter 12 support vector machine Chapter 13 neural network
R语言书籍学习03 《深入浅出R语言数据分析》-第十章 关联规则 第十一章 随机森林
建造者模式
学习位段(1)
剑指Offer 29.顺时针打印矩阵
Latex combat notes 3- macro package and control commands
JVM | local method interface; Native Method Stack
67. 二进制求和
RPA丨首席财务官如何找到数字化转型“超级入口”?
Binary search - Learning
Leetcode-322- change exchange
R语言书籍学习03 《深入浅出R语言数据分析》-第七章 线性回归模型
Leetcode - 第2天
每日一题 -- 验证回文串
Dynamic memory management (1)
The same efficiency tool for leading enterprises to promote smart finance. Let's have a quick look?