当前位置:网站首页>Merge sort
Merge sort
2022-06-10 23:57:00 【Li_ XiaoJin】
Merge and sort related contents .
Merge sort is a typical application of divide and conquer , And it is a stable sort method , But you need to use extra space .
The idea of merging and sorting :
- Divide the array into subsequences , Cut length only 2 perhaps 1 The subsequence .
- Then use the temporary array , Sort subsequences , Merge , Then copy the values of the temporary array back to the original array .
- Repeat the operation 1~2 step , Until sorting is complete .
The advantage of merge sorting is that the time complexity of the best case and the worst case is O(nlogn), So it is a relatively stable sorting method .
https://lixj.fun/upload/2021/07/%E5%BD%92%E5%B9%B6%E6%8E%92%E5%BA%8F-2697907b116b410c923b1b70a71bc1f6.gif
Algorithm complexity :O(nlogn)
Algorithm space complexity :O(n)
Algorithm stability : Stable
public class MergeSortNew {
public static void mergeSort(int[] arr) {
if (arr == null || arr.length <= 1) {
return;
}
// int[] Array name = new int[ The length of the array ];
int[] temp = new int[arr.length];
// Merger
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;
// Merge the elements on the left
sort(arr, left, mid, temp);
// Merge right elements
sort(arr, mid + 1, right, temp);
// Merge subsequences
merge(arr, left, mid, right, temp);
}
}
private static void merge(int[] arr, int left, int mid, int right, int[] temp) {
// i Is the starting point of the first group , j Is the starting point of the second group
int i = left, j = mid + 1;
// m For the end of the first group , n For the end of the second group
int m = mid, n = right;
// k Used to point to temp Where is the array currently placed
int k = 0;
// Compare two ordered sequences circularly , Fill in array temp
while (i <= m && j <= n) {
if (arr[i] <= arr[j]) {
temp[k++] = arr[i++];
} else {
temp[k++] = arr[j++];
}
}
// If the comparison is over , There are still a few left in the first group , Then fill in all temp
while (i <= m) {
temp[k++] = arr[i++];
}
// If the comparison is over , There are still a few left in the second group , Then fill in all temp
while (j <= n) {
temp[k++] = arr[j++];
}
// Fill in the ordered numbers back to array The corresponding position of the 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: use Creative Commons signature 4.0 International license agreement to license Links:https://lixj.fun/archives/ Merge sort
边栏推荐
- Fiddler simulates low-speed network environment
- MySQL learning child query
- 【Pygame小游戏】首月破亿下载 一款高度融合了「超休闲游戏特性」的佳作~
- Easyrecovery15 simple and convenient data recovery tool
- 黑马头条丨腾讯薪酬制度改革引争议;英特尔全国扩招女工程师;黑马100%就业真的吗......
- LabVIEW uses the visa read function to read USB interrupt data
- Perfect decoding purecodec 20220601
- LabVIEW programming specification
- 【Turtle表白合集】“海底月是天上月,眼前人是心上人。”余生多喜乐,长平安~(附3款源码)
- Common settings for vs
猜你喜欢

Common settings for vs

LabVIEW在波形图或波形图表上显示时间和日期

LabVIEW改变Point ROI Overlay的形状或颜色

LabVIEW使用MathScript Node或MATLAB脚本时出现错误1046

VS的常用设置

LabVIEW获取IMAQ Get Last Event坐标

High speed data stream disk for LabVIEW

LabVIEW 禁止其他可多核心处理的应用程序在所有核心上执行

【Pygame小游戏】不怕你走不过系列:极致AI走迷宫,学习完带你打开新世界大门~(附游戏源码)

The serial port in the visa test panel under LabVIEW or max does not work
随机推荐
启牛学堂理财可靠吗,安全吗
LabVIEW使用MathScript Node或MATLAB脚本时出现错误1046
【Pygame小游戏】来了来了它来了——这款五子棋小游戏超A的,分享给你的小伙伴儿一起pk吧~
The serial port in the visa test panel under LabVIEW or max does not work
LabVIEW error "memory full - Application stopped on node"
LabVIEW获取Clamp函数找到的所有点的信息
Usage of C tryparse
快速排序
Basic introduction and core components of kubernetes
示波器刷新率怎么测量
LeetCode 501 :二叉搜索树中的众数
LabVIEW用VISA Read函数来读取USB中断数据
LabVIEW obtains the information of all points found by the clamp function
【LaTex】latex VS Code snippets(代码片段)
【Pygame小游戏】激荡大脑思维,一起来玩转奇思妙想“24点”叭~(超赞滴)
【Pygame小游戏】这款“打地鼠”小游戏要火了(来来来)
Baidu quick collection SEO optimization keyword ranking optimization skills
基于CenterOS7安装Redis及常见问题解决(带图讲解)
Flowable process deployment
LabVIEW获取IMAQ Get Last Event坐标