Java implementation of merge sort
Merge sort (MERGE-SORT) is an effective sorting algorithm based on the merge operation. This algorithm is a very typical use of the divide and conquer method (pide and Conquer). Applications. 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 two-way merge. The merging process is: compare the sizes of a[i] and b[j], if a[i]≤b[j], copy the element a[i] in the first ordered list to r[k] , and add 1 to i and k respectively; otherwise, copy the element b[j] in the second ordered list to r[k], and add 1 to j and k respectively, and so on, until After one ordered list is fetched, the remaining elements in the other ordered list are copied to the cells in r from subscript k to subscript t. We usually use recursion to implement the merge sorting algorithm. We first divide the interval to be sorted [s, t] into two at the midpoint, then sort the left sub-range, then sort the right sub-range, and finally use a merge operation between the left interval and the right interval. Merge into ordered intervals [s,t].
Two main points:
1How to merge two ordered sequence into one ordered sequence
2How to turn these two sequence into an ordered sequence
Solve the first problem:
How to merge two ordered sequences. This is very simple. Just compare the first number of the two sequence and take the smaller one first. After taking it, delete the number in the corresponding sequence. Then compare them. If one of the columns is empty, just take out the data of the other column one by one.
public static void merge(int[] nums, int low, int mid, int high) { int[] temp = new int[high - low + 1]; int i = low;// 左指针 int j = mid + 1;// 右指针 int k = 0; // 把较小的数先移到新数组中 while (i <p style="margin-top:0px; margin-bottom:16px; color:rgb(51,51,51); font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol'; font-size:14px; line-height:22.4px">The second question: Groups A and B can be divided into two groups. By analogy, when the separated group has only one data, it can be considered that the group has reached order, and then the two adjacent groups can be merged. In this way, merge sorting is completed by first recursively decomposing the array and then merging the array. </p><pre style="font-size:11.9px; line-height:1.45; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:normal; margin-top:0px; margin-bottom:16px; padding:16px; overflow:auto; color:rgb(51,51,51); background-color:rgb(247,247,247)">public static int[] sort(int[] nums, int low, int high) { int mid = (low + high) / 2; if (low <p style="margin-top:0px; margin-bottom:16px; color:rgb(51,51,51); font-family:'Helvetica Neue',Helvetica,'Segoe UI',Arial,freesans,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol'; font-size:14px; line-height:22.4px">Full code: </p><pre style="font-size:11.9px; line-height:1.45; font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace; word-wrap:normal; margin-top:0px; padding:16px; overflow:auto; color:rgb(51,51,51); margin-bottom:0px!important; background-color:rgb(247,247,247)">package algorithm;import java.util.Arrays;public class MergeSort { public static int[] sort(int[] nums, int low, int high) { int mid = (low + high) / 2; if (low <p> </p>
The above is the detailed content of What is merge sort? Detailed explanation of merge sort in Java. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于平衡二叉树(AVL树)的相关知识,AVL树本质上是带了平衡功能的二叉查找树,下面一起来看一下,希望对大家有帮助。

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。


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

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
