当前位置:网站首页>2.6归并排序
2.6归并排序
2022-07-30 04:16:00 【TUJC】
2.6、归并排序
2.6.1、归并排序介绍:
归并排序(MERGE-SORT)是利用归并的思想,实现的排序方法,该算法采用经典的分治(divide-and-conquer)策略(分治法,将问题分成一些小的问题然后递归求解,而治的阶段,则将分的阶段得到的各答案"修补"在一起,即分而治之)。
归并排序思想示意图
归并排序思想示意图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;
}
}
}
边栏推荐
- 函数的底层机制
- JQ source code analysis (environment)
- MySQL 操作语句大全(详细)
- mysql 结构、索引详解
- Unity3D Application模拟进入前后台及暂停
- 【驱动】udev为USB转4串口的每个串口起别名
- Drools (7): WorkBench
- Pytorch framework learning record 5 - the use of DataLoader
- error: The following untracked working tree files would be overwritten by
- Usage of exists in sql
猜你喜欢
Mini Program Graduation Works WeChat Second-hand Trading Mini Program Graduation Design Finished Works (3) Background Functions
After 5 years of Ali internship interview~
数据库概论 - MySQL的简单介绍
我的Go+语言初体验——祝福留言小系统,让她也可以感受到你的祝福
Usage of exists in sql
Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Products (6) Question Opening and Defense PPT
sql语句-如何以一个表中的数据为条件据查询另一个表中的数据
网页元素解析a标签
SSM框架简单介绍
Pytorch框架学习记录2——TensorBoard的使用
随机推荐
After 5 years of Ali internship interview~
LeetCode 114. Expand Binary Tree into Linked List (One Question Three Eats)
New interface - API interface for "Taote" keyword search
代码开源设计实现思路
MySQL 操作语句大全(详细)
国内首家沉浸式高逼真元宇宙,希元宇宙正式上线
Azure 开发者新闻快讯丨开发者7月大事记一览
SQL introduction of the first lecture -- MySQL 8.0.29 installation tutorial (Windows 64 - bit)
Roperties class configuration file & DOS to view the host network situation
函数的底层机制
The leap second that may cause the next "Millennium Bug" is boycotted by tech giants
sqlmap use tutorial Daquan command Daquan (graphics)
RRU、BBU、AAU
SQLSERVER merges subquery data into one field
【Untitled】
Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Products (1) Development Overview
(redistribute, special comprehensive experiment ospf area)
宇宙的尽头是银行?聊聊在银行做软件测试的那些事
为什么突然间麒麟 9000 5G 版本,又有库存了?
Charles 替换 接口响应信息