Merging sort Merge Sorts

Source: Internet
Author: User
Tags array sort arrays sorts

Merge (merge) sorting method is to combine two (or more than two) ordered tables into a new ordered table, that is, the ordered sequence is divided into several sub-sequences, each sub-sequence is ordered. Then the ordered subsequence is combined into a whole ordered sequence.

Merge sort is an efficient sorting algorithm based on the merging operation. This algorithm is a very typical application of the partition method (Divide and Conquer).

The ordered Subsequence is merged to obtain a fully ordered sequence, i.e., the order of each subsequence is ordered, and then the sequence of sub-sequences is ordered. If you combine two ordered tables into an ordered table, it is called a 2-way merge.

Example

Original state

First array [2 6]

Second array [1 4]

The third array of [...]



1th step, order from the first, the second array to take a number: 2 and 1

When the size is compared, the small one is placed in the third array, and this becomes the bottom

First array [2 6]

Second array [4]

Third Array [1]



2nd, proceed to the previous step, order from the first, the second array to fetch data, 2 and 4,

The same size will be small after the third array, the state as follows

First array [6]

Second array [4]

Third array [1 2]



3rd step, then repeat the previous step to change the smaller 4 into the third array after the following state

First array [6]

The second array of [...]

Third Array [1 2 4]



4th step, finally put 6, sorted

The first array of [...]

The second array of [...]

Third Array [1 2 4 6]

The Java code is as follows:

public class MergeSort {public static void main (string[] args) {//First two arrays are ordered arrays int[] data1 = {2, 6, 8, 10, 17};
		Int[] Data2 = {7, 9, 11, 14, 20};
		From small to large sort int[] Tempsmalltolarge = Smalltolarge (data1, data2);
		
		From large to small sort int[] Templargetosmall = Largetosmall (data1, data2); 
		Show (Tempsmalltolarge);
		System.out.println ("");
	Show (Templargetosmall); }//This method implements the merged array sort for small to large sort public static int[] Smalltolarge (int[] data1, int[] data2) {int[] temp = new Int[data1.leng
		th + data2.length];
		int i = 0, j = 0, iter = 0; for (; I < data1.length && J < Data2.length;)
				{if (Data1[i] <= data2[j]) {Temp[iter] = Data1[i];
				iter++;
			i++;
				}else {Temp[iter] = data2[j];
				iter++;
			j + +;
		}} for (; i < data1.length; i++, iter++) {Temp[iter] = Data1[i];
		} for (; J < Data2.length; J + +, iter++) {Temp[iter] = data2[j];
	} return temp; }//This method implements the sorting of the merged array from large to small sort public static int[] Largetosmall (int[] data1, int[] DaTA2) {int[] temp = new Int[data1.length + data2.length];
		int i = data1.length-1, j = data2.length-1, iter = 0; for (; I >= 0 && J >= 0;)
				{if (Data1[i] > Data2[j]) {Temp[iter] = Data1[i];
				iter++;
			i--;
				}else {Temp[iter] = data2[j];
				iter++;
			j--;
		}} for (; I >= 0; I--, iter++) {Temp[iter] = Data1[i];
		} for (; J >= 0; J--, iter++) {Temp[iter] = data2[j];
	} return temp;  }//auxiliary function public static void show (int[] temp) {for (int i = 0; i < temp.length; i++) {System.out.print (Temp[i]
		+ " ");
 }
	}
}


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.