Java Merge Sorting

Source: Internet
Author: User

The basic principle of Merge Sorting is:

I. Split. If there is a list of n elements, split the list into a new list composed of two or more elements, and sort the sublists separately.

Ii. Merge: merge all sub-tables with sorted orders. This is repeated until they are merged into a list with N orders.

Merge Sorting is actually a Sort Algorithm of the same idea as quick sorting. It uses the basic idea of divide and conquer, that is, to break down the problem into small ones and conquer it separately.

Public class mergesort {/** subscript of the first element of the Left array of leftpos * subscript of the last element of the Left array of leftend * subscript of the first element of the Right array * final of the right array of rightend subscript of an element */Private Static <integer extends comparable <? Super integer> void meger (INT [] A, int [] tmparray, int leftpos, int rightpos, int rightend) {int leftend = rightPos-1; int tmppos = leftpos; int numelements = rightend-leftpos + 1; // put the smallest two arrays into the array tmparrray while (leftpos <= leftend & rightpos <= rightend) {if (a [leftpos]-(A [rightpos]) <= 0) {tmparray [tmppos ++] = A [leftpos ++];} else {tmparray [tmppos ++] = A [rightpos ++] ;}// Add the elements of another array in the two arrays to temarray while (leftpos <= leftend ){ Tmparray [tmppos ++] = A [leftpos ++];} while (rightpos <= rightend) {tmparray [tmppos ++] = A [rightpos ++];} // copy the content in the intermediate array back to the original array for (INT I = 0; I <numelements; I ++, rightend --) {A [rightend] = tmparray [rightend];} for (INT I = 0; I <tmparray. length; I ++) {system. out. print (tmparray [I] + ",");} system. out. println ();} /** left index of the first element to be arranged in the array * Right index of the last element of the array to be arranged * A index of the left-side array of the array * after the tmparray is merged * subscript */Private Static <integer of the last element Extends comparable <? Super integer> void mergesort (INT [] A, int [] tmparray, int left, int right) {If (left <right) {int center = (left + right) /2; // recursion of the array on the left, during the recursion process until the array on the left is split into elements into a new array mergesort (A, tmparray, left, center ); // recursion of the array on the left. During the recursion process, the array on the right is split into elements into a new array mergesort (A, tmparray, center + 1, right ); // merge the array meger (A, tmparray, left, center + 1, right) ;}} public static void main (string [] ARGs) {mergesort sort = new mergesort (); int num [] = {5, 7, 3, 8, 9, 2, 6}; int num2 [] = new int [9]; sort. mergesort (Num, num2, 0, num. length-1); For (INT I = 0; I <num2.length; I ++) {system. out. print (num2 [I] + ",");} system. out. println (); For (INT I = 0; I <num2.length; I ++) {system. out. print (Num [I] + ",") ;}} the output result is: 5, 7, 0, 0, 0, 0, 0, 3, 5, 7, 0,, 0, 0, 8, 4, 9, 2, 6, 9, 9, after all arrays are merged, 7, 8, 9, after sorting the original array: 1, 2, 3, 4, 5, 6, 7, 8, 9,

Contact Us

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.