当前位置:网站首页>Sorting learning sorting
Sorting learning sorting
2022-07-01 14:09:00 【Zhemu】
Merge sort
Recursive :
Code :
#include<iostream>
using namespace std;
const int maxn=100010;
int a[maxn];
void merge(int a[],int L1,int R1,int L2,int R2){
int temp[maxn],index=0;// Zero time array temp, Used to store the sorted sequence ;
int i=L1;int j=L2;
while(i<=R1&&j<=R2){//R2 For the initial n-1, No n, You can wait ;
if(a[i]<=a[j]){
temp[index++]=a[i++];// Sort from small to large , Let the small one in first temp;
}else {
temp[index++]=a[j++];
}
}
while(i<=R1)temp[index++]=a[i++];// Because the length of the two sequences that may be merged is different , So the rest of the ratio is saved to temp Inside , The next step of recursion will continue to sort, so don't worry ;
while(j<=R2)temp[index++]=a[j++];
for(int i=0;i<index;i++){
a[L1+i]=temp[i];
}
}
void mergesort(int a[],int left,int right){
if(left<right){// Until there is only one element in each interval ;
int mid=left+(right-left)/2;
mergesort(a,left,mid);
mergesort(a,mid+1,right);
merge(a,left,mid,mid+1,right);// There is one on every floor merge Merge ;
}
}
int n;
int main(){
cin>>n;
for(int i=0;i<n;i++)cin>>a[i];
mergesort(a,0,n-1);
for(int i=0;i<n;i++){
cout<<a[i];
if(i!=n-1)cout<<" ";
}
return 0;
}Non recursive ( Shell Sort ):
#include<iostream>
using namespace std;
const int maxn=100010;
int a[maxn];
int main(){
int n,j;
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i];
}
for(int step=n/2;step>0;step/=2){
for(int i=step;i<n;i++){
int temp=a[i];
for(j=i-step;i>=0&&a[j]>temp;j-=step){
a[j+step]=a[j];
}
a[j+step]=temp;
}
}
for(int i=0;i<n;i++){
cout<<a[i];
if(i!=n-1)cout<<" ";
}
return 0;
}
The essence is to divide and conquer , It's all group sorting ;
边栏推荐
- Force deduction solution summary 241- design priority for operation expression
- [repair version] imitating the template of I love watching movies website / template of ocean CMS film and television system
- 学会使用LiveData和ViewModel,我相信会让你在写业务时变得轻松
- el-form-item 正则验证
- Enter the top six! Boyun's sales ranking in China's cloud management software market continues to rise
- C语言课程设计题目
- 使用CMD修复和恢复病毒感染文件
- 【修复版】仿我爱看电影网站模板/海洋CMS影视系统模板
- [Jianzhi offer] 55 - ii balanced binary tree
- App automation testing Kaiyuan platform appium runner
猜你喜欢

TexStudio使用教程

Kongsong (Xintong Institute) - cloud security capacity building and trend in the digital era

Etcd summary mechanism and usage scenarios

App automation testing Kaiyuan platform appium runner

What "hard core innovations" does Intel have in the first half of 2022? Just look at this picture!

sqlilabs less10
![[repair version] imitating the template of I love watching movies website / template of ocean CMS film and television system](/img/fa/15b1cc3a8a723ff34eb457af9f701e.jpg)
[repair version] imitating the template of I love watching movies website / template of ocean CMS film and television system
![[anwangbei 2021] Rev WP](/img/98/ea5c241e2b8f3ae4c76e1c75c9e0d1.png)
[anwangbei 2021] Rev WP

玩转MongoDB—搭建MongoDB集群

Open source internship experience sharing: openeuler software package reinforcement test
随机推荐
sqlilabs less10
【241. 为运算表达式设计优先级】
小程序-小程序图表库(F2图表库)
[repair version] imitating the template of I love watching movies website / template of ocean CMS film and television system
建立自己的网站(21)
Error:Kotlin: Module was compiled with an incompatible version of Kotlin. The binary version of its
In depth cooperation | Taosi data cooperates with changhongjia Huawei customers in China to provide tdengine with powerful enterprise level products and perfect service guarantee
开源实习经验分享:openEuler软件包加固测试
Go integrates logrus to realize log printing
介绍一种对 SAP GUI 里的收藏夹事务码管理工具增强的实现方案
清华章毓晋老师新书:2D视觉系统和图像技术(文末送5本)
6年技术迭代,阿里全球化出海&合规的挑战和探索
Effet halo - qui dit qu'il y a de la lumière sur la tête est un héros
被裁三个月,面试到处碰壁,心态已经开始崩了
Oracle-数据库对象的使用
Realize queue with stack and stack with queue (C language \leetcode\u 232+225)
Blind box NFT digital collection platform system development (build source code)
力扣解法汇总241-为运算表达式设计优先级
Admire, Ali female program undercover more than 500 black production groups
【NLP】预训练模型——GPT1