


What is the principle of merge sort algorithm in Java and how to implement it
1. Basic idea
Merge sort is an effective sorting algorithm based on merge operations. This algorithm is a very typical application using the divide and conquer method (Divide and Conquer). Merge the already ordered subsequences to obtain a completely ordered sequence; that is, first make each subsequence orderly, and then make the subsequence segments orderly. If two ordered lists are merged into one ordered list, it is called a 2-way merge.
2. Algorithm analysis
1. Algorithm description
Divide the input sequence of length n into two subsequences of length n/2; for these two subsequences Merge sorting is used respectively; two sorted subsequences are merged into a final sorting sequence.
2. Process Analysis
(1). Now we will split items [1] (index from 0 to 0, both sides included) and [28] index from 1 to 1, Both sides included) are merged together.
(2), because 1 (left split) <= 28 (right split), we copy {rightPart} into a new array.
(3) Since the left split is empty, we copy 28 (right split) into a new array.
(4). We copy the elements in the new array back to the original array.
(5), because 3 (left split) <= 21 (right split), we copy {rightPart} into a new array.
(6). Because the left split is empty, we copy 21 (right split) into a new array.
(7), Now we will split the terms [1,28] (index from 0 to 1, both sides included) and [3,21] with index from 2 to 3, both sides included) merged together.
(8), because 1 (left split) <= 3 (right split), we copy {rightPart} into a new array.
(9), because 28 (left split) > 3 (right split), we copy {rightPart} into a new array.
(10), because 28 (left split) > 21 (right split), we copy {rightPart} into a new array.
(11), because the right split is empty, we copy 28 (left split) into the new array.
# (12), we copy the elements in the new array back to the original array.
(13), Now we will split the terms [11] (index from 4 to 4, both sides included) and [7] with index from 5 to 5, both sides All included) are merged together.
(14), because 11 (left split) > 7 (right split), we copy {rightPart} into a new array.
(15), because the right split is empty, we copy 11 (left split) into the new array.
(16). We copy the elements in the new array back to the original array.
(17), and so on
(18), because 1 (left split) <= 6 (right split), we Copy {rightPart} into a new array.
(19), because 3 (left split) <= 6 (right split), we copy {rightPart} into a new array.
(20), because 21 (left split) > 6 (right split), we copy {rightPart} into a new array.
(21), because 21 (left split) > 7 (right split), we copy {rightPart} into a new array.
# (22), and so on, we copy the elements in the new array back to the original array.
3. GIF demonstration
3. Algorithm implementation
package com.algorithm.tenSortingAlgorithm; import java.util.Arrays; public class MergeSort { private static void mergeSort(int[] arr, int low, int high) { if (low < high) { //当子序列中只有一个元素时结束递归 int mid = (low + high) / 2; //划分子序列 mergeSort(arr, low, mid); //对左侧子序列进行递归排序 mergeSort(arr, mid + 1, high); //对右侧子序列进行递归排序 merge(arr, low, mid, high); //合并 } } private static void merge(int[] arr, int low, int mid, int high) { int[] temp = new int[arr.length]; //辅助数组 int k = 0, i = low, j = mid + 1; //i左边序列和j右边序列起始索引,k是存放指针 while (i <= mid && j <= high) { if (arr[i] <= arr[j]) { temp[k++] = arr[i++]; } else { temp[k++] = arr[j++]; } } //如果第一个序列未检测完,直接将后面所有元素加到合并的序列中 while (i <= mid) { temp[k++] = arr[i++]; } //同上 while (j <= high) { temp[k++] = arr[j++]; } //复制回原数组 for (int t = 0; t < k; t++) { arr[low + t] = temp[t]; } } public static void main(String[] args) { int[] arr = {1,28,3,21,11,7,6,18}; mergeSort(arr, 0, arr.length - 1); System.out.println(Arrays.toString(arr)); } }
The above is the detailed content of What is the principle of merge sort algorithm in Java and how to implement it. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Square Root in Java. Here we discuss how Square Root works in Java with example and its code implementation respectively.

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Guide to the Armstrong Number in Java. Here we discuss an introduction to Armstrong's number in java along with some of the code.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is
