You are on page 1of 4

Merge sort is a sorting algorithm based on the Divide and conquer strategy.

It works by recursively dividing the array into two equal halves, then sort
them and combine them.
it gets completed in three steps:

1. Divide: In this step, the array/list divides itself recursively into sub-arrays
until the base case is reached.
2. Recursively solve: Here, the sub-arrays are sorted using recursion.
3. Combine: This step makes use of the merge( ) function to combine the sub-
arrays into the final sorted array.

Algorithm for Merge Sort


Step 1: Find the middle index of the array.
Middle = 1 + (last – first)/2
Step 2: Divide the array from the middle.
Step 3: Call merge sort for the first half of the array
MergeSort(array, first, middle)
Step 4: Call merge sort for the second half of the array.
MergeSort(array, middle+1, last)
Step 5: Merge the two sorted halves into a single sorted array.
Example:
Radix sort or bucket sor is the linear sorting algorithm that is used for integers. In Radix sort,
there is digit by digit sorting is performed that is started from the least significant digit to
the most significant digit.

Working of raid sort:

1. Digits Place: In this step, The list is sorted based on digits’ place
2. Least Significant Bit sort: Initially, the list is sorted based on the Least
significant bit.
3. Most Significant bit: Step2 is repeated up to the most significant bit.

Algorithm for Merge Sort


Step 1: Find the number of digits in the maximum number of the list.

Step 2: Sort the list from the Least significant bit and place it into the
corresponding bucket.

Step 3: Step 2 is repeated up to the most significant bit.

Step 4: Finally, the sorted list will be formed.

Example:

Pass 1:
In the first pass, the list is sorted on the basis of the digits at 0's place.
After the first pass, the array elements are -

Pass 2:
In this pass, the list is sorted on the basis of the next significant digits (i.e., digits at
10th place).

After the second pass, the array elements are -

Pass 3:
In this pass, the list is sorted on the basis of the next significant digits (i.e., digits at
100th place).
After the third pass, the array elements are -

You might also like