当前位置:网站首页>排序第四节——归并排序(附有自己的视频讲解)
排序第四节——归并排序(附有自己的视频讲解)
2022-08-09 06:50:00 【学习追求高效率】
2.4 归并排序
***视频讲解
归并排序
模板
/** * 时间复杂度:O(n*logn) * 空间复杂度:O(n) * 稳定性:稳定排序 * 直接插入 冒泡 归并 * @param array */
public static void mergerSort1(int[] array) {
mergeSortFunc(array,0,array.length-1);
}
private static void mergeSortFunc(int[] array,int left,int right) {
if(left >= right) {
return;
}
int mid = (left+right) / 2;
//1、分解左边
mergeSortFunc(array,left,mid);
//2、分解右边
mergeSortFunc(array,mid+1,right);
//3、进行合并
merge(array,left,right,mid);
}
private static void merge(int[] array,int start,int end,
int midIndex) {
int[] tmpArr = new int[end-start+1];
int k = 0;//tmpArr数组的下标
int s1 = start;
int s2 = midIndex+1;
//两个归并段 都有数据
while (s1 <= midIndex && s2 <= end) {
if(array[s1] <= array[s2]) {
tmpArr[k++] = array[s1++];
}else {
tmpArr[k++] = array[s2++];
}
}
//当走到这里的时候 说明 有个归并段 当中 没有了数据 ,拷贝另一半的全部 到tmpArr数组当中
while (s1 <= midIndex) {
tmpArr[k++] = array[s1++];
}
while (s2 <= end) {
tmpArr[k++] = array[s2++];
}
//把排好序的数字 拷贝回 原数组
for (int i = 0; i < k; i++) {
array[i+start] = tmpArr[i];
}
}
2.4.1 基本思想
归并排序(MERGE-SORT)是建立在归并操作上的一种有效的排序算法,该算法是采用分治法(Divide and
Conquer)的一个非常典型的应用。将已有序的子序列合并,得到完全有序的序列;即先使每个子序列有序,再使子序列段间有序。若将两个有序表合并成一个有序表,称为二路归并。 归并排序核心步骤:
非递归模板
public static void mergerSort(int[] array) {
int gap = 1;//每组的数据
while (gap < array.length) {
for (int i = 0; i < array.length; i += gap*2 ) {
//进入这个循环 i一定合法
int s1 = i;
int e1 = s1+gap-1;
if(e1 >= array.length) {
e1 = array.length-1;
}
int s2 = e1+1;
if(s2 >= array.length) {
s2 = array.length-1;
}/**/
int e2 = s2+gap-1;
//int e2 = e1+gap;
if(e2 >= array.length) {
e2 = array.length-1;
}
merge(array,s1,e2,e1);
}
gap *= 2;
}
}
2.4.2 归并排序总结
- 归并的缺点在于需要O(N)的空间复杂度,归并排序的思考更多的是解决在磁盘中的外排序问题。
- 时间复杂度:O(N*logN)
- 空间复杂度:O(N)
- 稳定性:稳定
2.4.3 海量数据的排序问题
外部排序:排序过程需要在磁盘等外部存储进行的排序
前提:内存只有 1G,需要排序的数据有 100G
因为内存中因为无法把所有数据全部放下,所以需要外部排序,而归并排序是最常用的外部排序
- 先把文件切分成 200 份,每个 512 M
- 分别对 512 M 排序,因为内存已经可以放的下,所以任意排序方式都可以
- 进行 2路归并,同时对 200 份有序文件做归并过程,最终结果就有序了
边栏推荐
- VS2019常用快捷键
- Teach you how to make the Tanabata meteor shower in C language - elegant and timeless (detailed tutorial)
- 输入框最前面添加放大镜&&background-image和background-color冲突问题
- cut命令的使用实例
- Search 1688 product interface by image (item_search_img-search 1688 product by image (Politao interface) code docking tutorial
- Fragments
- 报错:flask: TypeError: ‘function‘ object is not iterable
- Use baidu EasyDL intelligent bin
- The solution that does not work and does not take effect after VScode installs ESlint
- mysql 总结
猜你喜欢

The working principle of the transformer (illustration, schematic explanation, understand at a glance)

错误:为 repo ‘oracle_linux_repo‘ 下载元数据失败 : Cannot download repomd.xml: Cannot download repodata/repomd.

代码目录结构

C语言的内置宏(定义日志宏)

longest substring without repeating characters

线程的6种状态

Redis 2 - 高级

报错:FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS重大开销和将disab补充道

Use baidu EasyDL intelligent bin

C# 利用iTextSharp画PDF
随机推荐
Search 1688 product interface by image (item_search_img-search 1688 product by image (Politao interface) code docking tutorial
XxlJobConfig分布式定时器任务管理XxlJob配置类,替代
The water problem of leetcode
Teach you how to make the Tanabata meteor shower in C language - elegant and timeless (detailed tutorial)
shardingsphere数据分片配置项说明和示例
单例 DCL(double check lock) 饱汉模式和饿汉模式
way of thinking problem-solving skills
【Oracle 11g】Redhat 6.5 安装 Oracle11g
leetcode 之 70 爬楼梯问题 (斐波那契数)
TCP段重组PDU
eyb:Redis学习(2)
golang xml 处理动态属性
Service
2022年7月小结
当酷雷曼VR直播遇上视频号,会摩擦出怎样的火花?
Redis 2 - 高级
io.lettuce.core。RedisCommandTimeoutException命令超时
为什么以太网无法接收大于1500字节的数据包?
2022-08-08: Given an array arr, it represents the height of the missiles that will appear in order from morning to night.When the cannon shoots missiles, once the cannon is set to shoot at a certain h
高项 03 项目立项管理

