Merging sort _java of Java sort algorithm summary

Source: Internet
Author: User

This article illustrates the merging sort of Java sort algorithm summary. Share to everyone for your reference. The specific analysis is as follows:

The merge operation (merge), also called the merge algorithm, refers to the operation of merging two sorted sequences into one sequence. Similar to the quick sort, let's look at merging implementations in Java.

Merge sorting (merge) is the merging of two (or more) ordered tables into a new ordered table, which divides the sequences to be sorted into several subgroups, each of which is ordered. Then the ordered Subsequence is merged into the whole ordered sequence.

Merge ordering is an effective sorting algorithm based on merging operation. The algorithm is a very typical application of the partition method (Divide and Conquer). The ordered Subsequence is merged to obtain a fully ordered sequence, that is, the sequence of each subsequence is ordered, and then the sequence between the subsequence segments is ordered. If the two ordered table is merged into an ordered table, it is called 2-way merge.

The merging sort algorithm is stable, the array needs the extra space O (n), the extra space of O (n) is needed in the list, the time complexity is O (Nlog (n)), the algorithm is not adaptive, and the random reading of the data is not required.

Working principle:

1, the application space, so that its size is the sum of two sorted sequence, the space used to store the merged sequence
2, set two pointers, the initial position is two already sorted the starting position of the sequence
3. Compare the elements pointed to by two pointers, select the relatively small elements into the merged 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

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;
  Recmergesort (WorkSpace, lowerbound, mid);
  Recmergesort (WorkSpace, mid+1, upperbound);
 Merge (WorkSpace, lowerbound, mid+1, upperbound);
 } private void Merge (long[] workspace,int lowptr,int highptr,int upperbound) {int j = 0;
 int lowerbound = lowptr;
 int mid = highPtr-1;
 int n = upperbound-lowerbound+1; while (Lowptr<=mid&&highptr<=upperbound) {if (Thearray[lowptr]<thearray[highptr]) {workSpace[j++]
  =thearray[lowptr++];
  } else{workspace[j++]=thearray[highptr++];
 } while (Lowptr<=mid) {workspace[j++] = thearray[lowptr++];
 while (Highptr<=upperbound) {workspace[j++] = thearray[highptr++];
 for (j=0;j<n;j++) {thearray[lowerbound+j]=workspace[j]; }
} 

Merge sort is a relatively stable sort. That is, the order of the elements that are equal will not change. The output is 2 if you enter records 1 (1) 3 (2) 3 (2) 4 (5) 5 (1) (the keywords in parentheses are records) (1) 2 (3) 2 (4) 3 (2) 5 (5) 2 and 2 are in the order in which they are entered. To sort the number It is important to require that other information be sorted in the order in which it is entered, according to which one of the information is to be ordered. This is also where it is more advantageous than the quick sort.

I hope this article will help you with your Java programming.

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.