What is Merge Sort Algorithm: How does it work, its Advantages and Disadvantages
Who doesn't want to work on one of the most respected algorithms, popularly known as merge sort?
The merge sort strategy emphasizes dividing a bigger problem into subproblems so as to make it easier for the programmer to solve them.
This concept works by combining the solutions to the sub-problems in order to come to the final conclusion and get the required result.
Though, a clear understanding of this concept is vital for every programmer. Read this blog till the end to learn all about the advantages and disadvantages of the merge sort algorithm, and merge two sorted array, and to get a gist of the next permutation.
What is a Merge Sort Algorithm?
Merge sort is a sorting algorithm that splits the items of an array in the sorted order into two groups. It further recursively sorts each group and merges them into a final sorted sequence.
As we have discussed above this method is useful for tackling bigger problems since you can first divide them into finer sub-problems and solve them one at a time.
For instance, if you have been given to sort a group of elements as a problem that are represented in a line, we will have to consider every single element as a single component of the problem. Here, anything more than one will be considered a bigger problem.
Let us take a small example and see how the working procedure of the merge sort algorithm goes:
- Sort the elements table given below:
1 2 3 4 5 6 7 8
9 | 3 | 7 | 5 | 6 | 4 | 8 | 2 |
A
mid
Let’s start with the algorithm for this problem,
Here are the parameters to solve the above problem:
- Firstly we have to consider the beginning and the end of the list i.e l and h which in this case are 9 and 2 respectively
- Now we have to check if the low is less than high, which is this case is not true
- Next if the low if smaller than the high we will find the mid by adding the two elements and dividing it by two, i.e mid = (l+h)/2 which is this will land us on the number 5
- So this divides the list into two different halves, the left and the right hand side
Comments
Post a Comment