当前位置:网站首页>归并排序
归并排序
2022-06-10 22:38:00 【Li_XiaoJin】
归并排序相关内容。
归并排序是采用分治法的典型应用,而且是一种稳定的排序方式,不过需要使用到额外的空间。
归并排序的思路:
- 把数组不断划分成子序列,划成长度只有2或者1的子序列。
- 然后利用临时数组,对子序列进行排序,合并,再把临时数组的值复制回原数组。
- 反复操作1~2步骤,直到排序完成。
归并排序的优点在于最好情况和最坏的情况的时间复杂度都是O(nlogn),所以是比较稳定的排序方式。
https://lixj.fun/upload/2021/07/%E5%BD%92%E5%B9%B6%E6%8E%92%E5%BA%8F-2697907b116b410c923b1b70a71bc1f6.gif
算法复杂度:O(nlogn)
算法空间复杂度:O(n)
算法稳定性:稳定
public class MergeSortNew {
public static void mergeSort(int[] arr) {
if (arr == null || arr.length <= 1) {
return;
}
// int[] 数组名 = new int[数组长度];
int[] temp = new int[arr.length];
// 归并
sort(arr, 0, arr.length - 1, temp);
}
public static void sort(int[] arr, int left, int right, int[] temp) {
if (left < right) {
int mid = (left + right) / 2;
// 归并左边元素
sort(arr, left, mid, temp);
// 归并右边元素
sort(arr, mid + 1, right, temp);
// 合并子序列
merge(arr, left, mid, right, temp);
}
}
private static void merge(int[] arr, int left, int mid, int right, int[] temp) {
// i为第一组的起点, j为第二组的起点
int i = left, j = mid + 1;
// m为第一组的终点, n为第二组的终点
int m = mid, n = right;
// k用于指向temp数组当前放到哪个位置
int k = 0;
// 将两个有序序列循环比较, 填入数组temp
while (i <= m && j <= n) {
if (arr[i] <= arr[j]) {
temp[k++] = arr[i++];
} else {
temp[k++] = arr[j++];
}
}
// 如果比较完毕, 第一组还有数剩下, 则全部填入temp
while (i <= m) {
temp[k++] = arr[i++];
}
// 如果比较完毕, 第二组还有数剩下, 则全部填入temp
while (j <= n) {
temp[k++] = arr[j++];
}
// 将排好序的数填回到array数组的对应位置
for (i = 0; i < k; i++) {
arr[left + i] = temp[i];
}
}
public static void main(String[] args) {
int[] arr = {10, 7, 2, 4, 7, 62, 3, 4, 2, 1, 8, 9, 19};
mergeSort(arr);
System.out.println(Arrays.toString(arr));
}
}
Copyright: 采用 知识共享署名4.0 国际许可协议进行许可 Links:https://lixj.fun/archives/归并排序
边栏推荐
- OpenVP*整合ldap認證
- Fiddler simulates low-speed network environment
- This article introduces you to j.u.c's futuretask, fork/join framework and BlockingQueue
- 给线程池里面线程添加名称的4种方式
- Mathematics and quality education
- Is it safe for CICC Fortune Securities to open an account? Is it reliable?
- Data and information resource sharing platform (6)
- 可扩展到Max–MCU和MPU开发,使用相同的许可证
- 宝塔计划任务Shell脚本定时删除某各目录下所有文件【记录】
- Perfect decoding purecodec 20220601
猜你喜欢

LabVIEW performs a serial loopback test

LabVIEW obtains the information of all points found by the clamp function

Apple CMS collection station source code - building tutorial - attached source code - new source code - development documents

LeetCode+ 16 - 20

SystemVerilog(十)-用户自定义类型

考研英语词汇 unit1

PHP implementation of iframe cross site text replacement / iframe website text replacement

im即时通讯源码带教程/uniapp即时通讯源码,附安装教程

自制APP连接OneNET---实现数据监控和下发控制(MQTT)

Dell r730 RAID5 installation server 2016 (disk larger than 2t)
随机推荐
Halcon combined with C # to detect surface defects -- affine transformation (II)
R language to draw two-dimensional normal distribution density surface;
Before we learn about high-performance computing, let's take a look at its history
Dell R730 raid5 安装Server 2016(解决磁盘大于2T)
Common settings for vs
LabVIEW错误“内存已满 - 应用程序停止在节点”
示波器和频谱分析仪的区别
LabVIEW锁相环(PLL)
Four ways to add names to threads in the thread pool
一文带你了解J.U.C的FutureTask、Fork/Join框架和BlockingQueue
LabVIEW或MAX下的VISA测试面板中串口无法工作
LabVIEW obtains the information of all points found by the clamp function
iframe框架自适应大小/全屏显示网页框架的方法
LabVIEW displays the time and date on the waveform chart or waveform chart
LabVIEW编程规范
Data and information resource sharing platform (VII)
Excel essential toolbox 17.0 Free Edition
Redis list list common commands
LabVIEW在波形图或波形图表上显示时间和日期
Flowable process deployment