当前位置:网站首页>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 ;
边栏推荐
- [IOT design. Part I] stm32+ smart cloud aiot+ laboratory security monitoring system
- AnimeSR:可学习的降质算子与新的真实世界动漫VSR数据集
- 自定义注解实现验证信息的功能
- Applet - applet chart Library (F2 chart Library)
- How can we protect our passwords?
- 开源实习经验分享:openEuler软件包加固测试
- So programmers make so much money doing private work? It's really delicious
- leetcode622. Design cycle queue (C language)
- Play with grpc - communication between different programming languages
- C语言订餐管理系统
猜你喜欢
佩服,阿里女程序卧底 500 多个黑产群……
QT社团管理系统
2022 PMP project management examination agile knowledge points (6)
Etcd 概要 机制 和使用场景
[IOT completion. Part 2] stm32+ smart cloud aiot+ laboratory security monitoring system
Six years of technology iteration, challenges and exploration of Alibaba's globalization and compliance
How will the surging tide of digitalization overturn the future?
TDengine 连接器上线 Google Data Studio 应用商店
sqlilabs less-11~12
QT community management system
随机推荐
队列的基本操作(C语言实现)
学会使用LiveData和ViewModel,我相信会让你在写业务时变得轻松
GET请求如何传递数组参数
Sign APK with command line
Leetcode(69)——x 的平方根
那个很努力的学生,高考失败了……别慌!你还有一次逆袭机会!
玩转gRPC—不同编程语言间通信
“国防七子”经费暴增,清华足足362亿元,甩第二名101亿 |全国高校2022预算大公开...
phpcms实现订单直接支付宝支付功能
"National defense seven sons" funding soared, with Tsinghua reaching 36.2 billion yuan, ranking second with 10.1 billion yuan. The 2022 budget of national colleges and universities was made public
当主程架构游戏的时候,防止到处调用减少耦合性,怎么开放接口给其他人调用呢?
【NLP】预训练模型——GPT1
Use lambda function URL + cloudfront to realize S3 image back to source
uni-app实现广告滚动条
TDengine 连接器上线 Google Data Studio 应用商店
SWT / anr problem - how to open binder trace (bindertraces) when sending anr / SWT
建立自己的网站(21)
When the main process architecture game, to prevent calls everywhere to reduce coupling, how to open the interface to others to call?
Leetcode question 1: sum of two numbers (3 languages)
C语言基础知识