The example of this article tells the merger and sorting of the Java sort algorithm summary. Share it for everyone for your reference. The specific analysis is as follows:
Merge operation (Merge), also known as the merger algorithm, refers to merging two sequences that have been sorted into a sequence operation. Similar to fast sorting, let's look at the implementation of the merged in Java.
Merge sorting (Merge) is to merge the two (or more) or more ordering tables into a new order, that is, divide the sequence to be sorted into several sequences, and each subsequent sequence is orderly. Then combine the order sequence into the overall order.
Merge sorting is an effective sorting algorithm based on merger operations. This algorithm is a very typical application of Divide and Conquer. Merge the existing subsequent sequences to obtain a completely orderly sequence; that is, the sequence of each sub -sequence is orderly, and then the subsequent sequence segment is orderly. If the two ordered tables are merged into an orderly table, called 2-roads.
Merge sorting algorithms are stable, and array requires extra space of O (n). Links require extra space of O (LOG (N)). The time complexity is O (nlog (n)). Random read.
Working principle:
1. Apply for space to make the size of the two sorted sequences. The space is used to store the merged sequence
2. Set two pointers, and the initial positions are the starting position of two sorted sequences, respectively.
3. Compare the elements pointed by the two pointers, choose a relatively small element in the merger space, and move the pointer to the next location
4. Repeat step 3 until a pointer reaches the sequence end
5. Copy all the remaining elements in the other sequence directly to the tail of the merger sequence
Code implementation:
////////////////public void mergeSort(){ long[] workSpace = new long[nElems]; recMergeSort(workSpace,0,nElems-1);}private void recMergeSort(long [] Workspace, int Lowerbound, int UPPERBOUND) {if (LOWERBOUND == Upperbound) {Return;} Else {int Mid = (LOWERBOUND+UPPERBOUND)/2; RECMER. GESORT (Workspace, LOWERBOUND, MID); recomersort (Workspace, MID+ 1, upperbound); Merge (Workspace, LOWERBOUND, MID+1, Upperbound);}} Private Void Merge (Long [] workspace, int logtr, int Highptr, int Hupperbou nd) {int J = 0; int Lowerbound = lowPtr; int MID = HIGHPTR -1; int N = Upperbound -Lowerbound+1; While (Lowptr <= Mid && Highptr <= Uperbound) {IF (TheArray [Lowptr] <thearray [Highptr [Highptr] ]) {workspace [j ++] = TheaRray [Lowptr ++];} Else {workspace [j ++] = thearray [highptr ++];} while (lowPtr <= mid) {workspace [j ++] = thearray [lowPtr ++];} While (Highptr <= UPP Erbound) {workspace [j ++] = thearray [highptr ++] ;} for (j = 0; j <n; j ++) {thearray [LOWERBOUND+J] = Workspace [j];}}
Merge sorting is a relatively stable sort. The order of equal elements will not change. For example 2 and 2 in the 1 (1) 2 (3) 2 (4) 3 (2) 5 (5) output when the time output is in the order of input. Information sorting, it is important to require other information to be arranged in the order of input as much as possible. This is where it is more advantageous than fast sorting.
It is hoped that this article is helpful to everyone's Java program design.