merge sort program in c++ code example

Example 1: Merge sort in c++

#include <iostream>
using namespace std;
 

void merge(int arr[], int l, int m, int r)
{
    int n1 = m - l + 1;
    int n2 = r - m;
 
 
    int L[n1], R[n2];
 
   
    for (int i = 0; i < n1; i++)
        L[i] = arr[l + i];
    for (int j = 0; j < n2; j++)
        R[j] = arr[m + 1 + j];

 
    int i = 0;
 
    
    int j = 0;
 
    
    int k = l;
 
    while (i < n1 && j < n2) {
        if (L[i] <= R[j]) {
            arr[k] = L[i];
            i++;
        }
        else {
            arr[k] = R[j];
            j++;
        }
        k++;
    }
 
  
    while (i < n1) {
        arr[k] = L[i];
        i++;
        k++;
    }
 
   
    while (j < n2) {
        arr[k] = R[j];
        j++;
        k++;
    }
}
 

void mergeSort(int arr[],int l,int r){
    if(l>=r){
        return;
    }
    int m = (l+r-1)/2;
    mergeSort(arr,l,m);
    mergeSort(arr,m+1,r);
    merge(arr,l,m,r);
}
 

void printArray(int A[], int size)
{
    for (int i = 0; i < size; i++)
        cout << A[i] << " ";
}
 

int main()
{
    int arr[] = { 12, 11, 13, 5, 6, 7 };
    int arr_size = sizeof(arr) / sizeof(arr[0]);
 
    cout << "Given array is \n";
    printArray(arr, arr_size);
 
    mergeSort(arr, 0, arr_size - 1);
 
    cout << "\nSorted array is \n";
    printArray(arr, arr_size);
    return 0;
}

Example 2: merge sort

// @see https://www.youtube.com/watch?v=es2T6KY45cA&vl=en
// @see https://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html

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 3: merge sort c++

#include "tools.hpp"
/*   >>>>>>>> (Recursive function that sorts a sequence of) <<<<<<<<<<<< 
     >>>>>>>> (numbers in ascending order using the merge function) <<<<                                 */
std::vector<int> sort(size_t start, size_t length, const std::vector<int>& vec)
{
	if(vec.size()==0 ||vec.size() == 1)
	return vec;

	vector<int> left,right; //===>  creating left and right vectors 

	size_t mid_point = vec.size()/2; //===>   midle point between the left vector and the right vector 

	for(int i = 0 ; i < mid_point; ++i){left.emplace_back(vec[i]);} //===>  left vector 
	for(int j = mid_point; j < length; ++j){ right.emplace_back(vec[j]);} //===>  right vector 

	left = sort(start,mid_point,left); //===>  sorting the left vector 
	right = sort(mid_point,length-mid_point,right);//===>  sorting the right vector 
	

	return merge(left,right); //===>   all the function merge to merge between the left and the right
}
/*

>>>>> (function that merges two sorted vectors of numberss) <<<<<<<<<                                    */ 
vector<int> merge(const vector<int>& a, const vector<int>& b)
{
	vector<int> merged_a_b(a.size()+b.size(),0); // temp vector that includes both left and right vectors
	int i = 0;
	int j = 0;
	int k = 0;
	int left_size = a.size();
	int right_size = b.size();
	while(i<left_size && j<right_size) 
	{
		if(a[i]<b[j])
		{
			merged_a_b[k]=a[i];
			i++;
		}
		else
		{
			merged_a_b[k]=b[j];
			j++;
		}
		k++;
	}
	while(i<left_size)
	{
		merged_a_b[k]=a[i];
		i++;
		k++;
	}
	while(j<right_size)
	{
		merged_a_b[k]=b[j];
		j++;
		k++;
	}
	
	return merged_a_b;

}

Tags:

Java Example