# Merge Sort Algorithm in C++

Merge sort algorithm in c++ for beginners. This merge sorting program will merge or join the values which we have entered in the program.

This program works using the merging the element in the c++ programming language.

```#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)
{
mid=(low+high)/2;
merge_sort(arr,low,mid);
merge_sort(arr,mid+1,high);
merge(arr,low,high,mid);
}
}
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];
}
}
int main()
{
int myarray[30], num;
cout<<"Enter the numbers";
cin>>num;
cout<<"Enter "<<num<<" elements Sorted";
for (int i = 0; i < num; i++) { cin>>myarray[i];
}
merge_sort(myarray, 0, num-1);
cout<<"After sorting";
for (int i = 0; i < num; i++)
{
cout<<myarray[i]<<"\t";
}
}```

Output:

Enter the numbers

3

Enter 3 elements Sorted

23 45 78

After sorting 23 45 78

## Increment Operator in C++

The increment operator (++) returns a value after incrementing (adding one to) its operand.

Unary operators that increment or add 1 to the operand value are known as increment operators.

The double plus symbol (++) represents the Increment operand. Operators are divided into two categories: pre-increment and post-increment.

## Merge Sort C++ Definition

One of the most efficient sorting algorithms is merge sort. It is based on the divide-and-conquer strategy.

Merge sort repeatedly breaks down a list into several sublists until each sublist contains only one element, then merges those sublists into a sorted list.

## Function in C++

In C++, a function is a collection of statements that work together to complete a specific task. At least one function in every C/C++ program is named main.

## For Loop in C++

A for loop is a repetition control structure that lets you write a loop that needs to run a certain number of times quickly.

A for-loop (or simply for loop) is a control flow statement in computer science that specifies iteration and allows code to be executed repeatedly.

While-loops increment and test a loop variable, for-loops can be thought of as short hands for while-loops. Using this program we can execute merge sort algorithm in c++.