Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is Java merge sort

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

The main content of this article is to explain "what is the Java merge sort", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the Java merge sort"!

Basic introduction

Merging sorting (merge-sort) is a sorting method based on the idea of merging. The algorithm adopts the classical divide-and-conquer strategy (divide divides the problem into some small problems and then recursively solves them, while the conquer stage "patches" the answers obtained in different stages, that is, divide and conquer).

Schematic diagram

Description: we can see that this structure is very much like a complete binary tree. We use recursion to implement the merge sort in this paper (or we can use iterative method to achieve it). Stages can be understood as the process of recursively disassembling a molecular sequence.

Let's take a look at the treatment stage, we need to merge two ordered subsequences into an ordered sequence. For example, in the following figure, we need to merge two ordered subsequences, [4, 5, 7, 8] and [1, 2, 3, 3, 6, 6] into the final sequence [1, 2, 3, 4, 5, 6, 7, 8].

Code example package com.structures.sort; blic class MergeSort {public static void main (String [] args) {int [] arr = new int [80000]; for (int I = 0; I < 800000; iTunes +) {arr [I] = (int) (Math.random () * 8000000);} int [] temp = new int [arr.length]; long start = System.currentTimeMillis () MergeSort (arr,0,arr.length-1,temp); long end = System.currentTimeMillis (); System.out.println ("time consuming:" + ((end-start)) + "ms") / * time consuming: 15ms * /} / public static void mergeSort (int [] arr, int left, int right, int [] temp) {if (left < right) {int mid = (left + right) / 2; / / decompose mergeSort (arr, left, mid, temp) recursively to the left / / decompose mergeSort (arr, mid + 1, right, temp) recursively to the right; / / merge merge (arr, left, mid, right, temp) }} / * merge * @ param arr sorted original array * @ initial index of ordered sequence on the left side of param left * @ param mid intermediate index * @ param right right index * @ param temp do transit array * / public static void merge (int [] arr, int left, int mid, int right, int [] temp) {int I = left / / initialization I, the initial index of the ordered sequence on the left is int j = mid + 1 dominating / initializing j, and the initial index of the ordered sequence on the right is int t = 0 / / the current index pointing to the temp array / / (1) / / first populate the data on the left and right sides (ordered) into the temp array according to the rules / / until one side of the ordered sequence on the left and right sides has been processed, that is, all populated into the temp array while (I

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report