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

How to realize merge sorting in Java

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

In order to solve this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

Merge sorting (MERGE-SORT) is an effective sorting algorithm based on merge operation, which is a very typical application of divide-and-conquer (Divide and Conquer). The ordered subsequences are merged to get a completely ordered sequence, that is, each subsequence is ordered first, and then the subsequence segments are ordered. If two ordered tables are merged into one ordered table, it is called two-way merging.

Merging process

Compare the sizes of a [I] and b [j]. If a [I] ≤ b [j], copy the element a [I] from the first ordered table to r [k] and add 1 to I and k respectively. Otherwise, copy the element b [j] from the second ordered table to r [k] and add 1 to j and k respectively, and cycle until one of the ordered tables is finished, and then copy the remaining elements in the other ordered table to the unit in r from subscript k to subscript t. The algorithm of merging and sorting is usually implemented recursively. First, the interval to be sorted is dichotomized by the midpoint, then the left subinterval is sorted, and then the right subinterval is sorted. Finally, the left interval and the right interval are merged into an ordered interval by a merge operation.

Merge operation

Merge, also known as merging algorithm, refers to the method of merging two sequential sequences into one sequential sequence. For example, if there is a sequence of {6pc202, 100pr, 301, 38pr, 8, 1}

Initial state: 6, 202, 100, 301, 38, 8, 1

After the first merger: {6202}, {100301}, {8pr 38}, {1}, number of comparisons: 3

After the second merger: {6100202301}, {1Jing 8JI 38}, comparison times: 4

After the third merger: {1pm 6pm 8pm 38100202301}, comparison times: 4

The total number of comparisons is: 3 / 4 / 4 / 11

The reverse number is 14.

Algorithm description

The merge operation works as follows:

Step 1: apply for space so that it is the sum of two sorted sequences, which is used to store the merged sequence

Step 2: set two pointers, the initial position is the starting position of the two sorted sequences

Step 3: compare the elements pointed to by the two pointers, select the relatively small elements to put into the merge space, and move the pointer to the next location

Repeat step 3 until a pointer exceeds the end of the sequence

Copy all the remaining elements of another sequence directly to the end of the merge sequence

Compare

The merge sort is a stable sort. That is, the order of equal elements will not change. If the input record 1 (1) 3 (2) 2 (3) 2 (4) 5 (5) (the keyword of the record in parentheses), the 2 and 2 of the output 1 (1) 2 (3) 2 (4) 3 (2) 5 (5) are in the order of input. This is important for sorting data to contain multiple information and to sort by one of them, requiring other information to be arranged as much as possible in the order in which it is entered. This is where it has an advantage over quick sorting.

Sample code

Merging and sorting principle

The specific working principle of merge sorting is as follows (assuming that the sequence has n elements):

The sequence is merge for every two adjacent digits to form a floor sequence, each sequence contains two elements after sorting.

The above sequences are merged again to form a floor sequence, each containing four elements.

Repeat step 2 until all elements have been sorted

Sample code:

This is the answer to the question about how to merge and sort in Java. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.

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

Internet Technology

Wechat

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

12
Report