# Sort algorithm (Java language)--merge sort

Source: Internet
Author: User
Tags comparable

The basic operation in merge sort MergeSort is to combine two sorted tables. Because these two tables are sorted, if you put the output in a third table, the algorithm can be completed by sequencing the input data. The basic merging algorithm is to take two input arrays A and B, an output array C, and 3 counters actr, Bctr, cctr, and they are initially placed at the beginning of the corresponding array. The smaller of a[actr] and B[bctr] is copied to the next position in C, and the relevant counter advances one step forward. When one of the two input tables is exhausted, the remainder of the other table is copied to C.

The time to merge another sorted table is obviously linear, because up to N-1 comparisons, where n is the total number of elements. Each comparison adds an element to C, in addition to the final comparison.

Merge sort is easy to describe, if n=1, then only one element needs to be sorted, otherwise the first half of the data and the second half of the data are sorted recursively, the two pieces of data are sorted, then the merging algorithm is used to merge the two parts together. Compared with other O (NLOGN) sorting algorithms, the run time of the merge algorithm relies heavily on the relative overhead of comparing elements and moving elements in arrays and temporary arrays. These costs are language-related.

The following is the implementation code for the merge sort algorithm:

`Import Java.util.random;public class Sortalgorithm {/** * Merge sort * @param array array */public static <anytype extends Compa rable<? Super anytype>> void MergeSort (anytype[] array) {count=0; @SuppressWarnings ("Unchecked") anytype[] Tmparray = ( Anytype[]) New Comparable[array.length];mergesort (array, Tmparray, 0, array.length-1);} /** * Merge Sort recursive call internal method * @param array required to sort * @param tmparray the array that holds the merge result * @param the leftmost subscript of the left sub-array * @param the right sub-array's leftmost subscript */PR Ivate static <anytype extends Comparable<? Super anytype>> void MergeSort (anytype[] array, anytype[] tmparray, int left, int. right) {if (left < right) {int Center = (left + right)/2;mergesort (array, Tmparray, left, center), MergeSort (array, Tmparray, center + 1, right), merge (a Rray, Tmparray, left, center + 1, right);}} /** * Algorithm for merging two sub-arrays * @param array to be sorted * @param tmparray the array that holds the merge result * @param the leftmost subscript of the LeftPos sub-array * @param the second half of the Rightpos sub-array is opened Start subscript * @param rightend Sub-array the right subscript */private static <anytype extends Comparable<?Super anytype>> void Merge (anytype[] array, anytype[] tmparray, int leftpos, int rightpos,int rightend) {int leftend = Rightpos-1;int Tmppos = Leftpos;int numelements = rightend-leftpos + 1;//main loop while (LeftPos <= leftend &&amp ; Rightpos <= rightend) {if (Array[leftpos].compareto (Array[rightpos]) <= 0) {tmparray[tmppos++] = array[leftPos++] ;} else {tmparray[tmppos++] = array[rightpos++];}} while (LeftPos <= leftend) {tmparray[tmppos++] = array[leftpos++];} while (Rightpos <= rightend) {tmparray[tmppos++] = array[rightpos++];} Copy the contents of Tmparray to arrayfor (int i = 0; i < numelements; i++) {array[rightend] = tmparray[rightend];rightend--;}}  public static void Main (string[] args) {integer[] List1 = new integer[] {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};for (int i = 0; i < 10; i++) {System.out.print (List1[i] + "");} System.out.println (); MergeSort (List1); System.out.println ("After sorting:"), for (integer integer:list1) {System.out.print (integer + "");} System.out.println("\ n---------------------"); integer[] List2 = new INTEGER[20]; Random random = new random (), for (int i = 0; i < i++) {List2[i] = Random.nextint (20); System.out.print (List2[i] + "");} System.out.println (); MergeSort (LIST2); System.out.println ("After sorting:"), for (integer integer:list2) {System.out.print (integer + "");}}}`

Execution Result:

9 8 7 6 5 4 3 2 1 0
After sorting:
0 1 2 3 4 5 6 7 8 9
---------------------
0 6 18 7 18 7 2 0 6 10 9 16 19 10 15 8 3 19 10 18
After sorting:
0 0 2 3 6 6 7 7 8 9 10 10 10 15 16 18 18 18 19 19

Sort algorithm (Java language)--merge sort

Related Keywords:
Related Article

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.