当前位置:网站首页>2.6 Merge Sort
2.6 Merge Sort
2022-07-30 04:30:00 【TUJC】
2.6、归并排序
2.6.1、归并排序介绍:
归并排序(MERGE-SORT)By using the merge is,实现的排序方法,该算法采用经典的分治(divide-and-conquer)策略(分治法,将问题分成一些小的问题然后递归求解,And rule of the stage,则将分的阶段得到的各答案"修补"在一起,即分而治之).
归并排序思想示意图
归并排序思想示意图2-合并相邻有序子序列:
再来看看治阶段,我们需要将两个已经有序的子序列合并成一个有序序列,比如上图中的最后一次合并,要将[4,5,7.8]和[1,2,3,6]两个已经有序的子序列,合并为最终序列[1,2,3,4,5,6,7,8],来看下实现步骤
2.6.2、代码开发
给你一个数组, val arr = Array(8,4,5,7,1,3,6,2),请使用归并排序完成排序.
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
public class MergetSort {
public static void main(String[] args) {
//int arr[] = { 8, 4, 5, 7, 1, 3, 6, 2 }; //
//测试快排的执行速度
// 创建要给80000个的随机的数组
int[] arr = new int[8000000];
for (int i = 0; i < 8000000; i++) {
arr[i] = (int) (Math.random() * 8000000); // 生成一个[0, 8000000) 数
}
System.out.println("排序前");
Date data1 = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date1Str = simpleDateFormat.format(data1);
System.out.println("排序前的时间是=" + date1Str);
int temp[] = new int[arr.length]; //归并排序需要一个额外空间
mergeSort(arr, 0, arr.length - 1, temp);
Date data2 = new Date();
String date2Str = simpleDateFormat.format(data2);
System.out.println("排序前的时间是=" + date2Str);
//System.out.println("归并排序后=" + Arrays.toString(arr));
}
//分+合方法
public static void mergeSort(int[] arr, int left, int right, int[] temp) {
if(left < right) {
int mid = (left + right) / 2; //中间索引
//向左递归进行分解
mergeSort(arr, left, mid, temp);
//向右递归进行分解
mergeSort(arr, mid + 1, right, temp);
//合并
merge(arr, left, mid, right, temp);
}
}
//合并的方法
/** * * @param arr 排序的原始数组 * @param left 左边有序序列的初始索引 * @param mid 中间索引 * @param right 右边索引 * @param temp 做中转的数组 */
public static void merge(int[] arr, int left, int mid, int right, int[] temp) {
int i = left; // 初始化i, 左边有序序列的初始索引
int j = mid + 1; //初始化j, 右边有序序列的初始索引
int t = 0; // 指向temp数组的当前索引
//(一)
//先把左右两边(有序)的数据按照规则填充到temp数组
//直到左右两边的有序序列,有一边处理完毕为止
while (i <= mid && j <= right) {
//继续
//如果左边的有序序列的当前元素,小于等于右边有序序列的当前元素
//即将左边的当前元素,填充到 temp数组
//然后 t++, i++
if(arr[i] <= arr[j]) {
temp[t] = arr[i];
t += 1;
i += 1;
} else {
//反之,将右边有序序列的当前元素,填充到temp数组
temp[t] = arr[j];
t += 1;
j += 1;
}
}
//(二)
//把有剩余数据的一边的数据依次全部填充到temp
while( i <= mid) {
//左边的有序序列还有剩余的元素,就全部填充到temp
temp[t] = arr[i];
t += 1;
i += 1;
}
while( j <= right) {
//右边的有序序列还有剩余的元素,就全部填充到temp
temp[t] = arr[j];
t += 1;
j += 1;
}
//(三)
//将temp数组的元素拷贝到arr
//注意,并不是每次都拷贝所有
t = 0;
int tempLeft = left; //
//第一次合并 tempLeft = 0 , right = 1 // tempLeft = 2 right = 3 // tL=0 ri=3
//最后一次 tempLeft = 0 right = 7
while(tempLeft <= right) {
arr[tempLeft] = temp[t];
t += 1;
tempLeft += 1;
}
}
}
边栏推荐
- @WebServlet注解(Servlet注解)
- 骁龙7系芯片表现如何?Reno8 Pro佐证新一代神U
- How to compare struct, slice, map for equality and the difference between several comparison methods in golang
- LeetCode 114. Expand Binary Tree into Linked List (One Question Three Eats)
- WEB penetration of information collection
- Pytorch框架学习记录5——DataLoader的使用
- 文件系统二
- C. Qualification Rounds(思维,特情)
- PyG搭建R-GCN实现节点分类
- Android Studio 实现登录注册-源代码 (连接MySql数据库)
猜你喜欢

【软件工程之美 - 专栏笔记】31 | 软件测试要为产品质量负责吗?

A brief introduction to the SSM framework

Data Lake: Data Integration Tool DataX

Redis【超详解!!!】

The leap second that may cause the next "Millennium Bug" is boycotted by tech giants

My first experience of Go+ language——Blessing message system, so that she can also feel your blessings

新型LaaS协议Elephant Swap给ePLATO提供可持续溢价空间

国内首家沉浸式高逼真元宇宙,希元宇宙正式上线

labelme的使用技巧

Eureka Registry
随机推荐
《构建之法》笔记---第十章 典型用户和场景
精品MySQL面试题,备战八月99%必问!过不了面试算我的
Install MySQL Database on Kylin V10 Operating System
Redis "super explanation!!!!!!"
Android Studio implements login registration - source code (connecting to MySql database)
unity初学5 摄像机跟随,边界控制以及简单的粒子控制(2d)
Introduction to Thymeleaf
为什么突然间麒麟 9000 5G 版本,又有库存了?
Thinkphp 5.0.24 Variable Override Vulnerability Causes RCE Analysis
Charles 替换 接口响应信息
MySQL String Concatenation - Various String Concatenation Practical Cases
VUX Datetime 组件compute-days-function动态设置日期列表
RRU、BBU、AAU
Roperties class configuration file & DOS to view the host network situation
1. 获取数据-requests.get()
Roperties类配置文件&DOS查看主机网络情况
[The Mystery of Cloud Native] Cloud Native Background && Definition && Detailed explanation of related technologies?
MYSQL 唯一约束
Thymeleaf简介
[MRCTF2020]Hello_ misc