Table of Contents
1. Basic idea
2. Algorithm analysis
1. Algorithm description
2. Process Analysis
3. GIF demonstration
3. Algorithm implementation
Home Java javaTutorial What is the principle of merge sort algorithm in Java and how to implement it

What is the principle of merge sort algorithm in Java and how to implement it

Apr 19, 2023 pm 05:43 PM
java

    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.

    What is the principle of merge sort algorithm in Java and how to implement it

    (2), because 1 (left split) <= 28 (right split), we copy {rightPart} into a new array.

    What is the principle of merge sort algorithm in Java and how to implement it

    (3) Since the left split is empty, we copy 28 (right split) into a new array.

    What is the principle of merge sort algorithm in Java and how to implement it

    (4). We copy the elements in the new array back to the original array.

    What is the principle of merge sort algorithm in Java and how to implement it

    (5), because 3 (left split) <= 21 (right split), we copy {rightPart} into a new array.

    What is the principle of merge sort algorithm in Java and how to implement it

    (6). Because the left split is empty, we copy 21 (right split) into a new array.

    What is the principle of merge sort algorithm in Java and how to implement it

    (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.

    What is the principle of merge sort algorithm in Java and how to implement it

    (8), because 1 (left split) <= 3 (right split), we copy {rightPart} into a new array.

    What is the principle of merge sort algorithm in Java and how to implement it

    (9), because 28 (left split) > 3 (right split), we copy {rightPart} into a new array.

    What is the principle of merge sort algorithm in Java and how to implement it

    (10), because 28 (left split) > 21 (right split), we copy {rightPart} into a new array.

    What is the principle of merge sort algorithm in Java and how to implement it

    (11), because the right split is empty, we copy 28 (left split) into the new array.

    What is the principle of merge sort algorithm in Java and how to implement it

    # (12), we copy the elements in the new array back to the original array.

    What is the principle of merge sort algorithm in Java and how to implement it

    (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.

    What is the principle of merge sort algorithm in Java and how to implement it

    (14), because 11 (left split) > 7 (right split), we copy {rightPart} into a new array.

    What is the principle of merge sort algorithm in Java and how to implement it

    (15), because the right split is empty, we copy 11 (left split) into the new array.

    What is the principle of merge sort algorithm in Java and how to implement it

    (16). We copy the elements in the new array back to the original array.

    What is the principle of merge sort algorithm in Java and how to implement it

    (17), and so on

    (18), because 1 (left split) <= 6 (right split), we Copy {rightPart} into a new array.

    What is the principle of merge sort algorithm in Java and how to implement it

    (19), because 3 (left split) <= 6 (right split), we copy {rightPart} into a new array.

    What is the principle of merge sort algorithm in Java and how to implement it

    (20), because 21 (left split) > 6 (right split), we copy {rightPart} into a new array.

    What is the principle of merge sort algorithm in Java and how to implement it

    (21), because 21 (left split) > 7 (right split), we copy {rightPart} into a new array.

    What is the principle of merge sort algorithm in Java and how to implement it

    # (22), and so on, we copy the elements in the new array back to the original array.

    What is the principle of merge sort algorithm in Java and how to implement it

    3. GIF demonstration

    What is the principle of merge sort algorithm in Java and how to implement it

    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));
        }
    }
    Copy after login

    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!

    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. Best Graphic Settings
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. How to Fix Audio if You Can't Hear Anyone
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Square Root in Java Square Root in Java Aug 30, 2024 pm 04:26 PM

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

    Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

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

    Random Number Generator in Java Random Number Generator in Java Aug 30, 2024 pm 04:27 PM

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

    Armstrong Number in Java Armstrong Number in Java Aug 30, 2024 pm 04:26 PM

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

    Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

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

    Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

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

    Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

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

    Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

    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

    See all articles