Example 1: merge sort
#include <iostream>
using namespace std;
void merge(int arr[],int start,int mid,int end)
{
int n1=mid-start+1;
int n2=end-mid;
int l[n1],m[n2];
for(int i=0;i<n1;i++)
{
l[i]=arr[start+i];
}
for(int j=0;j<n2;j++)
{
m[j]=arr[mid+1+j];
}
int i=0;
int j=0;
int k=start;
while(i<n1&&j<n2)
{
if(l[i]<m[j])
{
arr[k]=l[i];
k++;
i++;
}
else
{
arr[k]=m[j];
k++;
j++;
}
}
while(i<n1)
{
arr[k]=l[i];
k++;
i++;
}
while(j<n2)
{
arr[k]=m[j];
k++;
j++;
}
}
void mergesort(int arr[],int start,int end)
{
if(start<end)
{
int mid=(start+end)/2;
mergesort(arr,start,mid);
mergesort(arr,mid+1,end);
merge(arr,start,mid,end);
}
}
void display(int arr[],int n)
{
for(int i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;
}
int main()
{
int n;
cout<<"enter the size of the array:"<<endl;
cin>>n;
cout<<"enter the elements of the array:"<<endl;
int arr[n];
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
cout<<"array as it is:"<<endl;
display(arr,n);
cout<<"sorted array:"<<endl;
mergesort(arr,0,n-1);
display(arr,n);
return 0;
}
Example 2: Merge sort
class Sort
{
void merge(int arr[], int left, int middle, int right)
{
int low = middle - left + 1;
int high = right - middle;
int L[] = new int[low];
int R[] = new int[high];
int i = 0, j = 0;
for (i = 0; i < low; i++)
{
L[i] = arr[left + i];
}
for (j = 0; j < high; j++)
{
R[j] = arr[middle + 1 + j];
}
int k = left;
i = 0;
j = 0;
while (i < low && j < high)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
while (i < low)
{
arr[k] = L[i];
i++;
k++;
}
while (j < high)
{
arr[k] = R[j];
j++;
k++;
}
}
void mergeSort(int arr[], int left, int right)
{
int middle;
if (left < right) {
middle = (left + right) / 2;
mergeSort(arr, left, middle);
mergeSort(arr, middle + 1, right);
merge(arr, left, middle, right);
}
}
void display(int arr[])
{
for (int i=0; i<arr.length; ++i)
{
System.out.print(arr[i]+" ");
}
}
public static void main(String args[])
{
int arr[] = { 9, 3, 1, 5, 13, 12 };
Sort ob = new Sort();
ob.mergeSort(arr, 0, arr.length - 1);
ob.display(arr);
}
}
Example 3: mergesort
def merge_sort(arr):
if len(arr) > 1:
middle = len(arr)
left = arr[:middle]
right = arr[middle:]
merge_sort(left)
merge_sort(right)
i = len(arr) - 1
while i >= 0:
if left and right:
if left[-1] >= right[-1]:
arr[i] = left.pop()
else:
arr[i] = right.pop()
else:
arr[i] = left.pop() if left else right.pop()
i -= 1
return arr
Example 4: merge sort
function merge(list, start, midpoint, end) {
const left = list.slice(start, midpoint);
const right = list.slice(midpoint, end);
for (let topLeft = 0, topRight = 0, i = start; i < end; i += 1) {
if (topLeft >= left.length) {
list[i] = right[topRight++];
} else if (topRight >= right.length) {
list[i] = left[topLeft++];
} else if (left[topLeft] < right[topRight]) {
list[i] = left[topLeft++];
} else {
list[i] = right[topRight++];
}
}
}
function mergesort(list, start = 0, end = undefined) {
if (end === undefined) {
end = list.length;
}
if (end - start > 1) {
const midpoint = ((end + start) / 2) >> 0;
mergesort(list, start, midpoint);
mergesort(list, midpoint, end);
merge(list, start, midpoint, end);
}
return list;
}
mergesort([4, 7, 2, 6, 4, 1, 8, 3]);
Example 5: merge sort
# Python3 recursive merge sort algorithm -> O(n*log(n))
def merge_sort(A):
def merge(l, r):
i = j = 0
n = [] # merging container
while i < len(l) or j < len(r):
# if no more elements to the right,
# add remaining left elements
if i == len(l):
n.extend(r[j:])
break
# if no more elements to the left,
# add remaining right elements
if j == len(r):
n.extend(l[i:])
break
# if elements left on both sides,
# add smaller element
a, b = l[i], r[j]
if a < b:
n.append(a)
i += 1
else:
n.append(b)
j += 1
return n
# divide list down to single-elements
s = len(A)
if s > 1:
s
l = merge_sort(A[:s]) # split left
r = merge_sort(A[s:]) # split right
return merge(l, r) # merge sides in order
else:
return A
Example 6: merge sort
Step 1 − if it is only one element in the list it is already sorted, return.
Step 2 − divide the list recursively into two halves until it can no more be divided.
Step 3 − merge the smaller lists into new list in sorted order.