Merge sort
Merge sort is the algorithm which follows divide and conquer approach. Consider an array A of n number of elements. The algorithm processes the elements in 3 steps.
- If A Contains 0 or 1 elements then it is already sorted, otherwise, Divide A into two sub-array of equal number of elements.
- Conquer means sort the two sub-arrays recursively using the merge sort.
- Combine the sub-arrays to form a single final sorted array maintaining the ordering of the array. The main idea behind merge sort is that, the short list takes less time to be sorted.
- A = {10, 5, 2, 23, 45, 21, 7}
- Step 1: [INITIALIZE] SET I = BEG, J = MID + 1, INDEX = 0
- Step 2: Repeat while (I <= MID) AND (J<=END)
IF ARR[I] < ARR[J]
SET TEMP[INDEX] = ARR[I]
SET I = I + 1
ELSE
SET TEMP[INDEX] = ARR[J]
SET J = J + 1
[END OF IF]
SET INDEX = INDEX + 1
[END OF LOOP]
Step 3: [Copy the remaining
elements of right sub-array, if
any]
IF I > MID
Repeat while J <= END
SET TEMP[INDEX] = ARR[J]
SET INDEX = INDEX + 1, SET J = J + 1
[END OF LOOP]
[Copy the remaining elements of
left sub-array, if any]
ELSE
Repeat while I <= MID
SET TEMP[INDEX] = ARR[I]
SET INDEX = INDEX + 1, SET I = I + 1
[END OF LOOP]
[END OF IF] - Step 4: [Copy the contents of TEMP back to ARR] SET K = 0
- Step 5: Repeat while K < INDEX
SET ARR[K] = TEMP[K]
SET K = K + 1
[END OF LOOP] - Step 6: Exit
- Step 1: IF BEG < END
SET MID = (BEG + END)/2
CALL MERGE_SORT (ARR, BEG, MID)
CALL MERGE_SORT (ARR, MID + 1, END)
MERGE (ARR, BEG, MID, END)
[END OF IF] - Step 2: END
- #include<stdio.h>
- void mergeSort(int[],int,int);
- void merge(int[],int,int,int);
- void main ()
- {
- int a[10]= {10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
- int i;
- mergeSort(a,0,9);
- printf("printing the sorted elements");
- for(i=0;i<10;i++)
- {
- printf("\n%d\n",a[i]);
- }
- }
- void mergeSort(int a[], int beg, int end)
- {
- int mid;
- if(beg<end)
- {
- mid = (beg+end)/2;
- mergeSort(a,beg,mid);
- mergeSort(a,mid+1,end);
- merge(a,beg,mid,end);
- }
- }
- void merge(int a[], int beg, int mid, int end)
- {
- int i=beg,j=mid+1,k,index = beg;
- int temp[10];
- while(i<=mid && j<=end)
- {
- if(a[i]<a[j])
- {
- temp[index] = a[i];
- i = i+1;
- }
- else
- {
- temp[index] = a[j];
- j = j+1;
- }
- index++;
- }
- if(i>mid)
- {
- while(j<=end)
- {
- temp[index] = a[j];
- index++;
- j++;
- }
- }
- else
- {
- while(i<=mid)
- {
- temp[index] = a[i];
- index++;
- i++;
- }
- }
- k = beg;
- while(k<index)
- {
- a[k]=temp[k];
- k++;
- }
- }
- public class MyMergeSort
- {
- void merge(int arr[], int beg, int mid, int end)
- {
- int l = mid - beg + 1;
- int r = end - mid;
- intLeftArray[] = new int [l];
- intRightArray[] = new int [r];
- for (int i=0; i<l; ++i)
- LeftArray[i] = arr[beg + i];
- for (int j=0; j<r; ++j)
- RightArray[j] = arr[mid + 1+ j];
- int i = 0, j = 0;
- int k = beg;
- while (i<l&&j<r)
- {
- if (LeftArray[i] <= RightArray[j])
- {
- arr[k] = LeftArray[i];
- i++;
- }
- else
- {
- arr[k] = RightArray[j];
- j++;
- }
- k++;
- }
- while (i<l)
- {
- arr[k] = LeftArray[i];
- i++;
- k++;
- }
- while (j<r)
- {
- arr[k] = RightArray[j];
- j++;
- k++;
- }
- }
- void sort(int arr[], int beg, int end)
- {
- if (beg<end)
- {
- int mid = (beg+end)/2;
- sort(arr, beg, mid);
- sort(arr , mid+1, end);
- merge(arr, beg, mid, end);
- }
- }
- public static void main(String args[])
- {
- intarr[] = {90,23,101,45,65,23,67,89,34,23};
- MyMergeSort ob = new MyMergeSort();
- ob.sort(arr, 0, arr.length-1);
- System.out.println("\nSorted array");
- for(int i =0; i<arr.length;i++)
- {
- System.out.println(arr[i]+"");
- }
- }
- }
- using System;
- public class MyMergeSort
- {
- void merge(int[] arr, int beg, int mid, int end)
- {
- int l = mid - beg + 1;
- int r = end - mid;
- int i,j;
- int[] LeftArray = new int [l];
- int[] RightArray = new int [r];
- for (i=0; i<l; ++i)
- LeftArray[i] = arr[beg + i];
- for (j=0; j<r; ++j)
- RightArray[j] = arr[mid + 1+ j];
- i = 0; j = 0;
- int k = beg;
- while (i < l && j < r)
- {
- if (LeftArray[i] <= RightArray[j])
- {
- arr[k] = LeftArray[i];
- i++;
- }
- else
- {
- arr[k] = RightArray[j];
- j++;
- }
- k++;
- }
- while (i < l)
- {
- arr[k] = LeftArray[i];
- i++;
- k++;
- }
- while (j < r)
- {
- arr[k] = RightArray[j];
- j++;
- k++;
- }
- }
- void sort(int[] arr, int beg, int end)
- {
- if (beg < end)
- {
- int mid = (beg+end)/2;
- sort(arr, beg, mid);
- sort(arr , mid+1, end);
- merge(arr, beg, mid, end);
- }
- }
- public static void Main()
- {
- int[] arr = {90,23,101,45,65,23,67,89,34,23};
- MyMergeSort ob = new MyMergeSort();
- ob.sort(arr, 0, 9);
- Console.WriteLine("\nSorted array");
- for(int i =0; i<10;i++)
- {
- Console.WriteLine(arr[i]+"");
- }
- }
- }
Complexity
Complexity | Best case | Average Case | Worst Case |
---|---|---|---|
Time Complexity | O(n log n) | O(n log n) | O(n log n) |
Space Complexity | O(n) |
Example :
Consider the following array of 7 elements. Sort the array by using merge sort.Algorithm
C Program
printing the sorted elements 7 9 10 12 23 23 34 44 78 101
Java Program
Sorted array 23 23 23 34 45 65 67 89 90 101
C# Program
Sorted array 23 23 23 34 45 65 67 89 90 101