Simple Merge Sort Program in C++
Definition
Merge sort is an O(n log n) comparison-based sorting algorithm. Most implementations produce a stable sort, which means that the implementation preserves the input order of equal elements in the sorted output. Conceptually, a merge sort works as: Divide the unsorted list into n sublists, each containing 1 element and repeatedly merge sublists to produce new sorted sublists until there is only 1 sublist remaining. This will be the sorted list.
Simple Merge Sort Program Example
/* Simple Merge Sort Program Using Functions and Array in C++*/
/* Data Structure C++ Programs,C++ Functions and Array Examples */
#include <iostream>
#include<conio.h>
#include<stdlib.h>
#define MAX_SIZE 5
using namespace std;
void merge_sort(int, int);
void merge_array(int, int, int, int);
int arr_sort[MAX_SIZE];
int main() {
int i;
cout << "Simple C++ Merge Sort Example - Functions and Array\n";
cout << "\nEnter " << MAX_SIZE << " Elements for Sorting : " << endl;
for (i = 0; i < MAX_SIZE; i++)
cin >> arr_sort[i];
cout << "\nYour Data :";
for (i = 0; i < MAX_SIZE; i++) {
cout << "\t" << arr_sort[i];
}
merge_sort(0, MAX_SIZE - 1);
cout << "\n\nSorted Data :";
for (i = 0; i < MAX_SIZE; i++) {
cout << "\t" << arr_sort[i];
}
getch();
}
void merge_sort(int i, int j) {
int m;
if (i < j) {
m = (i + j) / 2;
merge_sort(i, m);
merge_sort(m + 1, j);
// Merging two arrays
merge_array(i, m, m + 1, j);
}
}
void merge_array(int a, int b, int c, int d) {
int t[50];
int i = a, j = c, k = 0;
while (i <= b && j <= d) {
if (arr_sort[i] < arr_sort[j])
t[k++] = arr_sort[i++];
else
t[k++] = arr_sort[j++];
}
//collect remaining elements
while (i <= b)
t[k++] = arr_sort[i++];
while (j <= d)
t[k++] = arr_sort[j++];
for (i = a, j = 0; i <= d; i++, j++)
arr_sort[i] = t[j];
}
Sample Output
Simple Merge Sort Example - Functions and Array
Enter 5 Elements for Sorting
67
57
45
32
13
Your Data : 67 57 45 32 13
Sorted Data : 13 32 45 57 67
------------------
(program exited with code: 0)
Sorting Programs
- Simple Bubble Sort Program in C++
- Simple Bubble Sort Program using functions in C++
- Simple Insertion Sort Program in C++
- Simple Insertion Sort Program using functions in C++
- Simple Selection Sort Program in C++
- Simple Selection Sort Program using functions in C++
- Simple Quick Sort Program in C++
- Simple Merge Sort Program in C++
- Simple Shell Sort Program in C++
- Simple Shell Sort Program using functions in C++
- Simple Heap Sort Program in C++
Read More Articles
- Simple Merge Sort Program in C++
- Scope Resolution Operator In C++
- Simple Program for Virtual Functions Using C++ Programming
- Simple Class Example Program For Find Prime Number In C++
- Simple Example Program For Parameterized Constructor In C++
- Define Constructor in Outside Class Example Program In C++
- Simple Example Program For Copy Constructor In C++
- Simple Program for Function Overloading Using C++ Programming
- Simple Program for Single Inheritance Using C++ Programming
- Simple Program for Inline Function without Class Using C++ Programming
- Factorial Using Function Example Program In C++
- Simple Example Program For Constructor Overloading In C++
- Simple Example Program For Constructor In C++
- Simple Addition ( Add Two Integers ) Example Program
- Simple Example Program for Inline Function Using C++ Programming
- Simple Program for Read user Input Using cin
- Factorial Using Loop Example Program In C++
- Simple Stack Program in C++ Programming
- Simple Program for Friend Function Using C++ Programming
- Simple Program for Static Data and Member Function Using C++ Programming
- Simple Program for Unary Operator Overloading Using C++ Programming
- Simple Program for Multiple Inheritance Using C++ Programming
- Do While Loop Example Program In C++
- Simple Copy Constructor Example Program For Find Factorial In C++
- Simple Program for Function Template Using C++ Programming