Comments and examples of merged sorting algorithms

Source: Internet
Author: User
The merge algorithm adopts the idea of divide and conquer, that is, the problem is divided into n subproblems with a small scale and similar structure as the original problem. The subproblems are solved recursively, and then the results are merged, finally, the original problem is solved. The merge algorithm is divided into three parts. The first part is the decomposition, which breaks down the shipping problem into two arrays containing n/2 elements, then call the function recursively to solve the sorting problem of the two arrays.

The merge algorithm adopts the idea of divide and conquer, that is, the problem is divided into n subproblems with a small scale and similar structure as the original problem. The subproblems are solved recursively, and then the results are merged, finally, the original problem is solved.

The merge algorithm is divided into three parts. The first part is the decomposition, which breaks down the shipping problem into two arrays containing n/2 elements, then call the function recursively to solve the sorting problem of the two arrays. The last step is to re-combine the sorted arrays to make them an array in order.

In these three steps, a large amount of code is the merged code, in fact, we can think of it as a problem of arranging two piles of cards in ascending order into a pile of cards. each time we compare the top cards, if it is small, it will be taken and put into the output heap. repeat the comparison process until a pile of cards are empty, and then put another pile of cards directly into the output heap. the comparison process ends, it is not difficult to understand the code if you have figured out this process.

// Sort the two arrays in order: public static void Merge (int [] rawArray, int firstIndex, int middleIndex, int lastIndex) {// divide the elements pointed to by the middle into the previous array to int firstArrayCount = middleIndex-firstIndex + 1; int secondArrayCount = lastIndex-middleIndex; int [] firstArray = new int [firstArrayCount]; int [] secondArray = new int [secondArrayCount]; // Copy the elements of the original array to the two separate arrays for (int I = 0; I <firstArrayCount; I ++) {firstArray [I] = rawArray [firstIndex + I] ;}for (int I = 0; I <secondArrayCount; I ++) {secondArray [I] = rawArray [middleIndex + I + 1];} int firstArrayIndex = 0; int secondArrayIndex = 0; int rawArrayIndex = firstIndex; // start merging. // use the while condition to determine whether all arrays have been traversed, if an array has already been output, the while loop while (firstArrayIndex <firstArrayCount & secondArrayIndex <secondArrayCount) {if (firstArray [firstArrayIndex] <= secondArray [secondArrayIndex]) will jump out. {// assign the value to the corresponding position of the original array. after completion, the index is added with 1 rawArray [rawArrayIndex ++] = firstArray [firstArrayIndex ++];} else {rawArray [rawArrayIndex ++] = secondArray [secondArrayIndex ++] ;}// after jumping out of the while loop, all the arrays have been output, because the other array must be an array sorted by size, the remaining elements are assigned to the original array in a loop to \ while (firstArrayIndex <firstArrayCount) {rawArray [rawArrayIndex ++] = firstArray [firstArrayIndex ++];} while (secondArrayIndex <secondArrayCount) {rawArray [rawArrayIndex ++] = secondArray [secondArrayCount ++];}

After reading the above merge, there is nothing to do with the recursive call below. the Function splits the array and recursively calls the function to sort the sub-array, after the sub-array is sorted, the merge function is called to merge the array.

////////////Array to be sorted///Left border///Right borderPublic static void MergeSort (int [] rawArray, int firstIndex, int lastIndex) {if (firstIndex <lastIndex) {int middleIndex = (firstIndex + lastIndex)/2; // recursively call the MergeSort (rawArray, firstIndex, middleIndex) function; MergeSort (rawArray, middleIndex + 1, lastIndex); // Merge (rawArray, firstIndex, middleIndex, lastIndex );}}

Merging and sorting mainly reflects the idea of splitting and solving the problem, solving the subproblem by calling the function recursively, and finally merging the result to get the solution of the original problem.

Many people around us have special requirements on algorithm languages when learning data structures and algorithms. I personally think learning algorithms is a way of solving problems, rather than implementing specific code, this is the point ....

This article is available at http://www.nowamagic.net/librarys/veda/detail/248.

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.