# Write a c code section for merge sort algorithm animation

And to merge the subarrays, made by dividing the original array of n elements, a running time of O n will be required. Let us call these arrays as left half and right half respectively. In merge sort we follow the following steps: We take a variable p and store the starting index of our array in this.

Here is how the sorted subarrays will look like: Recursively sorted left half: [1, 4, 5, 6] Recursively sorted right half: [2, 3, 7] Finally, as per step 3, we will merge these 2 halves to create the final sorted array.

But breaking the orignal array into 2 smaller subarrays is not helping us in sorting the array. The concept of Divide and Conquer involves three steps: Divide the problem into multiple small problems.

Moreover, merge sort is of interest because it creates an excellent case study for one of the widely used techniques in Computer Science — divide and conquer. Now, the idea here is that an array with a single element is already sorted, so once we break the original array into subarrays which has only a single element, we have successfully broken down our problem into base problems.

Before jumping on to, how merge sort works and it's implementation, first lets understand what is the rule of Divide and Conquer? Well that was history, and a socio-political policy Divide and Rulebut the idea here is, if we can somehow divide a problem into smaller sub-problems, it becomes easier to eventually solve the whole problem.

### Merge sort program in java

In Merge Sort, the given unsorted array with n elements, is divided into n subarrays, each having one element, because a single element is always sorted in itself. Combine the solutions of the subproblems to find the solution of the actual problem. Then we divide these 2 subarrays again, just like we divided our main array and this continues. Well, we sorted them recursively using the same technique as above. Once we have these 2 sorted subarrays in place, the rest of the code simply merges the 2. Conclusion Spread the Knowledge Merge sort is one of the most powerful sorting algorithms. We pick the smaller one and put it in the final merged array and move the corresponding pointer. And then we have to merge all these sorted subarrays, step by step to form one single sorted array. Merge Sort Pseudo Code Before we get into the actual code, let us take a look at the pseudo code. This means that merge sort requires additional space. Recursively sort the left half array and the right half array. When Britishers came to India, they saw a country with different religions living in harmony, hard working but naive citizens, unity in diversity, and found it difficult to establish their empire. The concept of Divide and Conquer involves three steps: Divide the problem into multiple small problems. Conquer the subproblems by solving them. So, we do nothing in this case and simply return.

In merge sort, we break the given array midway, for example if the original array had 6 elements, then merge sort will break it down into two subarrays with 3 elements each. Divide and Conquer If we can break a single big problem into smaller sub-problems, solve the smaller sub-problems and combine their solutions to find the solution for the original big problem, it becomes easier to solve the whole problem.

Conclusion Spread the Knowledge Merge sort is one of the most powerful sorting algorithms. Let us understand the detailed steps involved in performing a merge sort in the above array: [6, 4, 5, 1, 2, 7, 3] is divided into [6, 4, 5, 1] and [2, 7, 3] [6, 4, 5, 1] is divided into [6, 4] and [5, 1] [6, 4] is divided into  and   is a single element array and so, is sorted.

Rated 5/10 based on 26 review