当前位置:网站首页>Merge sort
Merge sort
2022-07-24 04:26:00 【lonesomee】
Realization principle
Put two unordered “ Small ” All elements of the array are orderly filled into a new array , Finish sorting at the same time “ Merge ”.

First compare the first element in the two arrays , Fill the new array with smaller numbers ( Ascending )
Compare the next element of the array that has taken the data with the previous one that has not taken the data , Then fill the new array with smaller numbers
3 | 5 |
Repeat the same operation , Until the last one is put in
3 | 5 | 6 | 7 |
Divide and conquer strategy ( Divide and rule )
The actual array will be split repeatedly , Get a small array ( The length is 1), Then sort and merge
Divide and conquer
Divide the problem into small ones , And then solve it recursively , Then combine the problems obtained in different stages
recursive
Inside the method , Call the current method itself
For example, in a() Method a() Method
Only when calling internally a() Method a() Method , Will continue to run the external a() Method the rest of the code
Unskilled , Be careful with recursion , Otherwise, it will form “ Dead cycle ” effect
Realization thought
Split the array in two
In the middle : The first subscript of the upper array +( The maximum subscript of the upper array - The first subscript of the upper array )/2
left : The first subscript of the upper array - In the middle
On the right side : In the middle +1- The maximum subscript of the upper array
Implementation code
import java.util.Arrays;
import java.util.Random;
public class MergeSort {
/**
*
* @param array The original array
* @param left Left position
* @param right Right position
* @return New array after arrangement
*/
public static int[] mergeSort(int[] array ,int left ,int right){
// If the starting and ending subscripts are consistent, it means that the current array has only one element , We can't divide any more
if (left == right){
return new int[]{array[left]};// Return this array
}
// Determine the middle position
int middle = left + (right - left)/2;
// Left array value range From the left position of the upper array to the middle point
int[] leftArray = mergeSort(array ,left ,middle);
// Right array value range The middle point of the upper array is one bit to the right to the right
int[] rightArray = mergeSort(array ,middle + 1 ,right);
// The length of the reordered array is the length of the addition of the two arrays
int[] newArray = new int[leftArray.length + rightArray.length];
// Left array start subscript
int l = 0;
// Right array start subscript
int r = 0;
// New array start subscript
int m = 0;
// When the left array loop is completed or the right array is traversed, the loop ends
while (leftArray.length > l && rightArray.length > r){
// Compare the size of the two arrays and put the smaller elements into the new array , At the end of the operation , The subscript of the array with data movement and the new array +1
newArray[m++] = leftArray[l] > rightArray[r]?rightArray[r++]:leftArray[l++];
}
// After the loop ends, there will be the remaining array, in which all the elements will be put into the new array
while (l < leftArray.length){
newArray[m++] = leftArray[l++];
}
while (r < rightArray.length){
newArray[m++] = rightArray[r++];
}
return newArray;
}
public static void main(String[] args) {
Random rd = new Random();
// Generating arrays
int[] arr = new int[rd.nextInt(100)];
for (int i = 0; i < arr.length; i++) {
arr[i] = rd.nextInt(500);
}
System.out.println(Arrays.toString(arr));
// Calling method
int[] brr = mergeSort(arr ,0 ,arr.length-1);
System.out.println(Arrays.toString(brr));
}
}
边栏推荐
- ARP Spoofing protection of network security
- (零八)Flask有手就行——数据库迁移Flask-Migrate
- Jinglianwen technology provides 3D point cloud image annotation service
- Post it notes --46{hbuildx connect to night God simulator}
- How to change the direction of this gracefully
- [translation] announce krius -- accelerate your monitoring and adoption of kubernetes
- What is the general family of programmers working in first tier cities?
- To -.---
- 64 attention mechanism 10 chapters
- 归并排序(Merge sort)
猜你喜欢

Particle Designer: particle effect maker, which generates plist files and can be used normally in projects
![[dish of learning notes, dog learning C] Dachang written test, is that it?](/img/4c/71c7268e40f0e2a15f52083022d565.png)
[dish of learning notes, dog learning C] Dachang written test, is that it?

002_ Kubernetes installation configuration

IP second experiment mGRE OSPF

PMIX ERROR: ERROR in file gds_ds12_lock_pthread.c

Qt5.14_ Realize the free drag and drop combination function of vs2019 panel under mingw/msvc

Educational Codeforces Round 132 A - D

IPhone binding 163 mailbox solution
[hope to answer] the data cannot be synchronized correctly

How does the trend chart of spot silver change?
随机推荐
【2023芯动科技提前批笔试题】~ 题目及参考答案
J9 number theory: what is Web3.0? What are the characteristics of Web3.0?
归并排序(Merge sort)
发送数据1010_1发人员通过 字节的
Basic learning notes of C language
Introduction to the application fields and functions of bank virtual human technology
Send data 1010_ 1. The number of bytes passed by the sender
Live video | 37 how to use starrocks to realize user portrait analysis in mobile games
基于GL Pipeline与光线追踪技术的融合实现的台球模拟器
Page Jump and redirection in flask framework
《论文复现》BiDAF代码实现过程(3)模型建立
Er system, in Lin reply bit, count, successfully open r com change
Iqoo 10 series attacks originos original system to enhance mobile phone experience
仿今日头条实时新闻微信小程序项目源码
Ambire gas tank launches exclusive NFT launch
佳的性能和可靠性发起写入IIC协类型码和的参数是-4
dispatch_ Once's Secret
What is the general family of programmers working in first tier cities?
.gz的业务交互和对外服篇中我们通合多个模型
Good performance and reliability. The parameter that initiates writing IIC co type code and is -4