当前位置:网站首页>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));
}
}
边栏推荐
- ACM warm-up Exercise 4 in 2022 summer vacation (summary)
- Determined by hardware (see official 2 and
- Embedded system transplantation [6] - uboot source code structure
- Collection of test case design methods
- Baidu search cracking down on pirated websites: why Internet content infringement continues despite repeated prohibitions
- 嵌入式系统移植【6】——uboot源码结构
- Live video | 37 how to use starrocks to realize user portrait analysis in mobile games
- 短视频本地生活版块,有哪些新的机会存在?
- Basic learning notes of C language
- The problem of monkeys eating peaches in classic exercises of C language
猜你喜欢

Exploration of new mode of code free production

Collection of test case design methods

C language classic exercises to write a program to find all the perfects within 1000.

智能合约:发布一种ERC20代币

Where is the difficulty in attracting investment in the park? Inventory of difficulties and difficulties in attracting investment in industrial parks

数组力扣(持续更新)

基于GL Pipeline与光线追踪技术的融合实现的台球模拟器

Post it notes --46{hbuildx connect to night God simulator}

Ambire gas tank launches exclusive NFT launch

Codeforces Round #808 (Div. 2) A - D
随机推荐
[2023 core technology approval test questions in advance] ~ questions and reference answers
Shell syntax (2)
Qt5.14_MinGW/MSVC下实现VS2019面板自由拖拽组合功能
致-.-- -..- -
IP second experiment mGRE OSPF
C语言经典习题之编写一个程序,找出1000以内所有的完数。
发送数据1010_1发人员通过 字节的
iPhone手机绑定163邮箱解决方案
Chery arizer 8 products are powerful, and "all excellent" is not for nothing
[untitled]
What new opportunities exist in the short video local life section?
C language classic exercises to write a program to find all the perfects within 1000.
佳的性能和可靠性发起写入IIC协类型码和的参数是-4
[untitled]
Oracle的并行技术
C主机对IIC从来分别设置每足够的话,可下几位
Qt5.14_ Realize the free drag and drop combination function of vs2019 panel under mingw/msvc
J9 number theory: what is Web3.0? What are the characteristics of Web3.0?
Alibaba Taobao Department interview question: how does redis realize inventory deduction and prevent oversold?
Label smoothing