当前位置:网站首页>归并排序模板 & 理解
归并排序模板 & 理解
2022-06-24 20:32:00 【GHOSTANDBREAD】
理解:
归并排序是对区间内的数进行分治,然后在归并的排序。
通过递归的方式将一整个区间分治成每个区间只有一个数,一个数认为是有序的,无需排序。然后对区间进行两两合并,即二路归并。
不然发现,因为是两两分治,所以分治的层数是log2n
还可以直接用stable_sort函数进行排序,其内部实现是归并排序
代码:
vector数组版
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int n;
const int N = 1e6 + 10;
vector<int> a(N);
vector<int> tmp(N);
void mergesort(vector<int> &a, int l, int r) {
if(l >= r) return;
// 当一个区间分成n个区间时,每个区间只有一个数时,分治结束
int mid = (l + r) >> 1;
mergesort(a, l, mid);
// 不断一分为二的分治,所以复杂度为log2n,分治的层数为log2n
mergesort(a, mid + 1, r);
int k = 0, i = l, j = mid + 1;
while(i <= mid && j <= r) { // 二路归并
if(a[i] <= a[j]) tmp[k ++] = a[i ++];
else tmp[k ++] = a[j ++];
}
while(i <= mid) tmp[k ++] = a[i ++]; //将还剩余的直接加进去
while(j <= r) tmp[k ++] = a[j ++];
for(i = l, j = 0; i <= r; i ++, j ++) a[i] = tmp[j];
//[l, r]区间的数就是tmp数组里的数,直接覆盖即可
}
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] << " ";
return 0;
}
stable_sort函数版:
#include<iostream>
#include<algorithm>
using namespace std;
int n;
int main() {
cin >>n;
vector<int> a(n);
for(int i = 0; i < n; i ++) cin >> a[i];
stable_sort(a.begin(), a.end());
for(int i = 0; i < n; i ++) cout << a[i] << " ";
return 0;
}边栏推荐
- 2022 crane driver (limited to bridge crane) examination question bank simulated examination platform operation
- Convert MySQL query timestamp to date format
- Library management system code source code (php+css+js+mysql) complete code source code
- [practical series] full WiFi coverage at home
- Deploy a production cluster using Loki microservice pattern
- Super detailed description and derivation of convolution and deconvolution (deconvolution is also called transpose convolution and fractional step convolution)
- 对技术的乐观,正让戴尔取得比想象中更多的成就
- 指南针炒股软件怎么样?安全吗?
- Scala sample object
- Yasea APK Download Image
猜你喜欢

4年工作經驗,多線程間的5種通信方式都說不出來,你敢信?

丹麦技术大学首创将量子计算应用于能源系统潮流建模
![[redis realizes seckill service ②] solution to oversold problem](/img/b6/3073def06a56565894c28e49767e3e.png)
[redis realizes seckill service ②] solution to oversold problem

Text border format and text block of rich text

Heavyweight: the domestic ide was released, developed by Alibaba, and is completely open source! (high performance + high customization)

Text editor for QT project practice - Episode 10

丹麥技術大學首創將量子計算應用於能源系統潮流建模

图书馆管理系统代码源码(php+css+js+mysql) 完整的代码源码
![[microservices sentinel] sentinel quick start | building an image | starting the console](/img/88/a01c8120f6117f1b8e4463cf6f685f.png)
[microservices sentinel] sentinel quick start | building an image | starting the console

LLVM TargetPassConfig
随机推荐
bindservice方法实现音乐播放暂停
Sanic service startup failed
yasea apk 下载 镜像
Mobile security tool -dex2jar
Syntax highlighting of rich text
[microservices sentinel] sentinel quick start | building an image | starting the console
ImageView展示网络图片
A small crawler program written by beginners
2022r1 quick opening pressure vessel operation test questions and answers
Library management system code source code (php+css+js+mysql) complete code source code
【无标题】
我想问一下兴业证券怎么开户?通过链接办理股票开户安全吗
Mobile security tool jar
Text border format and text block of rich text
2022 crane driver (limited to bridge crane) examination question bank simulated examination platform operation
Scala template method pattern
Scala IO read by line
腾讯云WeCity丨产业联合 协同创新 共贺新春!
最新QQ微信域名防红PHP程序源码+强制跳转打开
Scala sample object