How to Implement Merge Sort in C++?

Merge Sort is one of the simplest sorting algorithms in C++. In this post we will implement it in C++ 15/16, using a std::sort() method which takes an algorithm parameter. You can download the complete C++ code for implementing merge sort below:

mergge sort c++

By Inquisitive IbisInquisitive Ibis on Feb 21, 2021
#include <iostream>
using namespace std;
void merge(int *,int, int , int );
void merge_sort(int *arr, int low, int high)
{
    int mid;
    if (low < high){
        //divide the array at mid and sort independently using merge sort
        mid=(low+high)/2;
        merge_sort(arr,low,mid);
        merge_sort(arr,mid+1,high);
        //merge or conquer sorted arrays
        merge(arr,low,high,mid);
    }
}
// Merge sort 
void merge(int *arr, int low, int high, int mid)
{
    int i, j, k, c[50];
    i = low;
    k = low;
    j = mid + 1;
    while (i <= mid && j <= high) {
        if (arr[i] < arr[j]) {
            c[k] = arr[i];
            k++;
            i++;
        }
        else  {
            c[k] = arr[j];
            k++;
            j++;
        }
    }
    while (i <= mid) {
        c[k] = arr[i];
        k++;
        i++;
    }
    while (j <= high) {
        c[k] = arr[j];
        k++;
        j++;
    }
    for (i = low; i < k; i++)  {
        arr[i] = c[i];
    }
}
// read input array and call mergesort
int main()
{
    int myarray[30], num;
    cout<<"Enter number of elements to be sorted:";
    cin>>num;
    cout<<"Enter "<<num<<" elements to be sorted:";
    for (int i = 0; i < num; i++) { cin>>myarray[i];
    }
    merge_sort(myarray, 0, num-1);
    cout<<"Sorted array\n";
    for (int i = 0; i < num; i++)
    {
        cout<<myarray[i]<<"\t";
    }
}

Source: www.softwaretestinghelp.com

Add Comment

0

Merge sort is a sorting algorithm that utilizes quick and almost constant memory use.

C++ answers related to "mergge sort c++"

View All C++ queries

C++ queries related to "mergge sort c++"

mergge sort c++ sort char array c++ using insertion sort Write a program to sort an array 100,200,20, 75,89.198, 345,56,34,35 using Bubble Sort. The program should be able to display total number of passes used for sorted data in given data set. sort char array c++ using insertion sort descending order how to sort a vector in reverse c++ how to sort an array c++ how to sort in descending order c++ how to sort a vector in c++ vector sort in reverse order c++ sort in descending order c++ stl how to sort a string in c++ sort a string alphabetically c++ bucket sort algorithm c++ simple -vector reverse sort cpp bubble sort in c++ c++ how to sort numbers in ascending order binary sort c++ how to sort vector in c++ c++ sort function time complexity sort vector struct c++ how to sort an array in c++ c++ sort array of ints define my own compare function sort C++ stl how to sort in descending order in c++ sort vector descending sort a vector of strings according to their length c++ sort string vector of words alphabetically c++ merge sort . Shell sort in c++ vector sort c++ The number of swaps required in selection sort stl sort in c++ how to make a selection sort C++ sort vector in descending order c++ sort std vector sort what is time complexity of insertion sort Heap sort in c++ array sort c++ insertion sort in c++ program merge sort in c++ sort function in cpp merge sort code in c++ sort vector c++ quick sort in c++ how to sort array in c++ bubble sort c++ template Radix Sort in c++ quick sort predefined function in c++ c++ set sort order code for bubble sort in c++ c++ sort merge sort c++ vector topological sort cp algorithms sort inbuilt function in c++ sort vector of strings c++ stl sort insertion sort in c++ sort a vector c++ sort vector of pairs c++ heap sort heapify and max heap in binary tree sort tuple c++ turbo sort codechef solution c++ buble sort sort strings by length and by alphabet sort n characters in descending order c++ sort using comparator anonymous function c++ how to sort string containing numbers in c++ Sort by the distance between pairs c++ c++ bubble sort heap sort internal implementation using c++ extra parameter in comparator function for sort write a c++ program that reads ten strings and store them in array of strings, sort them and finally print the sorted strings sort using lambda c++ sort vector in descending order c++ merge sort c++ github sort in descending order c++ how to sort a vector bubble sort program in c++ sort in c++ sort c++ c++ sort vector of objects by property merge sort in c sort function sort vector topological sort Bubble Sort C++ c++ sort vector of objects by property.

Browse Other Code Languages

CodeProZone