# Write ac program to implement merge sort

It is the best Sorting technique used for sorting Linked Lists.

## Merge sort example

We again do what we did above — pick the smaller one and put it in the final merged array and move the corresponding pointer. 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 [6] and [4] [6] is a single element array and so, is sorted. Merge the left half array and right half-array to get the full array sorted. Final merged and sorted array: [1, 2, 3, 4, 5, 6, 7] The left and the right halves can always be sorted recursively using the same algorithm. This means that merge sort requires additional space. Both these sub-arrays are already sorted so we can simply merge them by using the technique explained above to get the sorted array [2, 3, 7]. And to merge the subarrays, made by dividing the original array of n elements, a running time of O n will be required. 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. And we take another variable r and store the last index of array in it. Let us take an example: Given Array: [6, 4, 5, 1, 2, 7, 3] First, as per step 1 above, we divide the array into 2 parts.

This means that merge sort requires additional space. Let us call these arrays as left half and right half respectively.

And to merge the subarrays, made by dividing the original array of n elements, a running time of O n will be required. Once we have divided the main array into subarrays with single elements, then we start merging the subarrays.

### Merge sort algorithm with example

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. We again do what we did above — pick the smaller one and put it in the final merged array and move the corresponding pointer. Combine the solutions of the subproblems to find the solution of the actual problem. And we take another variable r and store the last index of array in it. 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. It is also a stable sort, which means the "equal" elements are ordered in the same order in the sorted list. Both these sub-arrays are already sorted so we can simply merge them by using the technique explained above to get the sorted array [2, 3, 7]. To sort it, we will break it again into 2 sub-arrays: [2, 7] and [3].

It is also a stable sort, which means the "equal" elements are ordered in the same order in the sorted list. In the above example, we are given 2 arrays [1, 4, 5, 6] and [2, 3, 7].

Another question that is to be answered — how were the left and the right arrays sorted?

## Merge sort in c using recursion

Both these sub-arrays are already sorted so we can simply merge them by using the technique explained above to get the sorted array [2, 3, 7]. The magic happens in creating the final merged and sorted array. 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 [6] and [4] [6] is a single element array and so, is sorted. We will depict the pointer by underlining the corresponding element where the pointer points to. As we can see, the following are the subarrays left half and right half: Left half: [6, 4, 5, 1] Right half: [2, 7, 3] Then, as per step 2 above, we recursively sort the left and right halves. Before jumping on to, how merge sort works and it's implementation, first lets understand what is the rule of Divide and Conquer? 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. And to merge the subarrays, made by dividing the original array of n elements, a running time of O n will be required. Merge sort is widely used in various applications as well. To sort it, we will break it again into 2 sub-arrays: [2, 7] and [3]. 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. It is the best Sorting technique used for sorting Linked Lists.

Rated 6/10
based on 6 review

Download