(High efficiency ranking algorithm one) and sorted

Source: Internet
Author: User

Merge sort merge sort is an efficient sorting algorithm based on merging operations, which 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 two ordered tables are combined into an ordered table, they are called two-way merging. The merge process is: Compare the size of a[i] and a[j], if A[I]≤A[J], then the first ordered table elements a[i] copied to r[k], and I and K plus 1; otherwise, the second ordered TABLE element A[j] copied to R[k], and the J and K respectively add 1, This loop continues until one of the ordered tables is finished, and then the remaining elements in the other ordered table are copied to the cells in R from subscript K to subscript t. Merging sorting algorithm we usually use recursive implementation, first to sort the interval [s,t] to the midpoint of the two points, then the left sub-range, then the right sub-range is sorted, and finally the left and right intervals with a merge operation into an orderly interval [s,t].
Chinese name:Merge SortForeign name:Merge SortStability:StableComplexity of Time:O ( N log N) Complexity of space:O (n)Inventor:John von Neumann(Speed is second only to fast sequencing, stable sorting algorithm, generally used for the overall disorder, but the relative order of the various sub-series, Application see 2011 Universal 3rd "Swiss Wheel" of the standard)
Quick SortThe process effect is as follows, write it again next time


Merge operation merge (merge), also called merge algorithm, refers to the method of merging two sequential sequences into a sequential sequence. If there is a sequence {6,202,100,301,38,8,1} initial state: 6,202,100,301,38,8,1 after first merge: {6,202},{100,301},{8,38},{1}, number of comparisons: 3; After the second merge: { 6,100,202,301},{1,8,38}, number of comparisons: 4; After the third merge: {1,6,8,38,100,202,301}, Comparison: 4; The total number of comparisons is: 3+4+4=11, the reverse is 14;
Here's the map I'm looking for online.

Of course, I write the following program, arrays or the same number of groups, decomposition of the time is directly according to the minimum decomposition, is directly broken down by the most fine granularity
Package Data;import java.util.arrays;import java.util.random;/** * and sorted * @author JYC506 * */public class MergeSort {/ * * Merge partially sequenced arrays * @param arr to manipulate array * @param start1 array Part 1 beginning * @param end1 array Part 1 end point * @para M Start2 Array Part 2 beginning * @param end2 array Part 2 end point * @return */private Static int[] merger (int[] arr, int start1 , int end1, int start2, int end2) {int[] newArr = new int[(END1-START1) + (END2-START2) + 2];int index1 = Start1;int I Ndex2 = Start2;int index = 0;/* compares two arrays of sorted parts, starting from the beginning of these two parts, compared to smaller insert new arrays such as compare a[i] and a[j], if A[I]≤A[J], then the first ordered TABLE element A[i] Copy to r[ K], and let I and K respectively add 1, otherwise the elements in the second ordered table A[j] copied to R[k], and the J and K respectively plus 1, so loop down until one of the ordered table is finished */while (index1 <= end1 && index2 <= end2) {if (Arr[index1] < Arr[index2]) {Newarr[index] = arr[index1];index++;index1++;} else {Newarr[index] = Arr[i ndex2];index++;index2++;}} /* Then copy the remaining elements from the other ordered table to the cell in R from subscript K to subscript t */while (index1 <= end1) {Newarr[index] = arr[index1];index++;index1++;} while (Index2 <= End2) {Newarr[index] = arr[index2];index++;index2++;}    return NEWARR;} /** * Merges partially and sequentially sorted arrays * @param arr array to manipulate * @param start1 array Part 1 beginning * @param start2 sorted Array Part 2 beginning * @param end2 Array Part 2 end point */private static void merger (int[] arr, int start1, int start2, int end2) {int end1 = Start2 -1;int[] NewArr = Merger (arr, Start1, End1, Start2, End2);    System.arraycopy (NEWARR, 0, arr, Start1, newarr.length);} /** * and sort * @param arr array to manipulate * @param start start coordinates * @param number of elements in each group after size grouping */private static void Merg Ersort (int[] arr, int start, int size) {/* because it is a pair comparison, so multiply 2*/int length=arr.length;int dsize=size*2;int num=length/dsize; The int residue=length%dsize;//merges to the end of an ordered set, and is the time when the remainder is 0 if (num==0) {return;} Make a trip merge (note that the first time and return to only one element, that is, the 22 comparison is already sorted) for (int i=0;i<num;i++) {int sta=start+ (size*2) *i;merger (arr,sta,sta+s ize,sta+size*2-1);} Merge the remaining number and the last ordered set (this should be understood) if (residue!=0) {merger (arr,length-residue-size*2,length-residue,length-1);} Recursively executes the nextThe merge sort, and the element division multiplied mergersort (arr, 0, 2 * size);} /** * * @param arr to manipulate an array of */public static void Mergersort (int[] arr) {/* default starting coordinates of 0, and grouping elements for one start, because one is not ordered */mergersort (arr, 0,1);} public static void Main (string[] args) {/* Test and return to sort */int[] ar1=new int[]{10,4,6,3,8,2,5}; Mergesort.mergersort (AR1); System.out.println (arrays.tostring (AR1));/* Test 1 million random numbers and sort and Java comes with quick sort */int size=1000000;int[] arr1=new int[size]; Int[] Arr2=new int[size]; Random ran=new random (); for (int i=0;i<size;i++) {int data=ran.nextint (size); arr1[i]=data;arr2[i]=data;} Long Start=system.currenttimemillis (); Mergesort.mergersort (arr1); long End1=system.currenttimemillis (); Arrays.sort (ARR2); long End2=system.currenttimemillis (); System.out.println ((End1-start)); System.out.println ((END2-END1));}}
The operation results are as follows

Fast sorting algorithm is really faster than the algorithm.

(High efficiency ranking algorithm one) and sorted

Related Article

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.