Merge sort: merge sort is an efficient sorting algorithm based on merging operations , which is a very typical application using the divide-and-conquer 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 .
In the learning process of data structure, we often encounter sort algorithm, in which merge sort is a kind of efficient and simple algorithm complexity. In the textbook introduction, most will introduce the merging sorting algorithm. However, every time you read a book always feel easy, when you try to achieve, always error. Learning data structure has been a while, but directly let me write the merge sort code, also need to spend a lot of time to debug. Isn't there a better way for us to remember the code?
In fact, as long as we write code, pay attention to the skill, we can easily implement the merge sorting algorithm. Here are some of the ways I use it:
The first step: first write a merge two sorted array method, the method name is called Merge, as follows:
public static void merge (int[] A, int asize, int[] b, int bsize, int[] c) {int Tempa = 0, TEMPB = 0, TEMPC = 0;while (Tempa < asize && TEMPB < bsize) { if (A[tempa] > B[TEMPB]) { c[tempc++] = b[tempb++];} else{ c[tempc++] = a[tempa++];}} while (Tempa < asize) { c[tempc++] = a[tempa++];} while (TEMPB < bsize) { c[tempc++] = b[tempb++];}}
This method is very simple, a total of 5 parameters (can also be simplified to 3 parameters), where a, b array is to be merged array, asize,bsize is the array length (these two parameters can be removed), C is the target array. The main process is to constantly compare the size of a, b array, and then copy the smaller data into C. The key point here is to use 3 temporary variables to flag the corresponding position of each array, which can greatly simplify our code design. Here is the corresponding diagram process:
With this method, we can begin to write the main method of merging sorting. Writing the main body method is also very simple, the idea is divide and conquer the algorithm.
- The first step is to divide a large array into two small arrays
- The second part: sort the two arrays, using a recursive ordering method, that is, calling yourself
- Part III: Call the Merge method above to merge.
The code is very simple, directly affixed to the
public static void MergeSort (int[] source) { //recursive egress if (source.length = = 1) return;//divides large arrays into two decimal groups int middle = Source.length/2;int[] left = new int[middle];for (int i=0; i<middle; i++) { left[i] = source[i];} Int[] right = new int[source.length-middle];for (int i=middle; i<source.length; i++) { Right[i-middle] = source[i] ;} Sort the data (recursion is used here) mergesort (left);//mergesort merge sorted data merge (left, Left.length, right, right.length, source );}
Summary: Remember the core core idea of merge sorting algorithm: Divide and conquer.
How to implement merge sorting using recursion