Learn a bit every day algorithm-merge sort algorithm __ algorithm

Source: Internet
Author: User
Merge Sort Algorithm


definition


Merge sort (merge sort, Taiwan translation: Combined sorting) is an efficient sorting algorithm based on merging operations. This algorithm is a very typical application of the partition method (Divide and Conquer).


Steps


1. Apply the space so that it is the sum of two sorted sequences that are used to store the merged sequence

2. Set two pointers, the initial position is the starting position of two sorted series

3. Compare the elements pointed to by two pointers, select a relatively small element into the merge space, and move the pointer to the next position

4. Repeat step 3 until a pointer reaches the end of the sequence

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


Complexity of Time


O (NLOGN)


Code


Package com.sprd.test.algorithm; /** * Copyright TJ spreadtrum TEST_AF All right Reserved * * @author: Hui.qian Created on November 27, 2014 morning 9:09:13 D Escription: */public class MergeSort {public int[] sort (int[] A, int[] b) {int[] merge = new Int[a.length + B.leng
		TH];
		int indexa = 0;
		int INDEXB = 0;
		int i = 0; while (Indexa < a.length && Indexb < b.length) {if (A[indexa] <= B[indexb]) {merge[i++] = A[inde
			Xa++];
			} else {merge[i++] = b[indexb++];
			}} if (indexa = = A.length) {for (int j = Indexb; Indexb < b.length; indexb++) {merge[i++] = B[indexb];
			}} else {for (int j = indexa; indexa < a.length; indexa++) {merge[i++] = B[indexa];
	}} return merge;
		} public static void Main (string[] args) {int[] sorta = {1, 3, 5, 7, 9};
		Int[] SORTB = {2, 4, 6, 8, 10};
		MergeSort sorter = new MergeSort ();
		Long start = System.currenttimemillis ();
		int[] sorted = Sorter.sort (sorta, SORTB); LOng end = System.currenttimemillis ();
		SYSTEM.OUT.PRINTLN ("Time Consuming:" + (End-start));
		System.out.println ("Before sorting:");
		print (sorta);
		Print (SORTB);
		System.out.println ("After sorting:");
	print (sorted);  public static void print (int[] datas) {for (int i = 0; i < datas.length; i++) {System.out.print (Datas[i] + "
		");
	} System.out.println ("");
 }
}

Output


Time: 0
before sequencing: 
1 3 5 7 9 
2 4 6 8 
after sorting: 


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.