I am new to c++ and was trying develop a code for merge sort. I tested it with a sample array of size 5 but the answer put out by the code is not right. I can't figure what's going wrong. Here is my code:
#include <iostream>
#include <cstring>
#include <sstream>
#include <fstream>
#include <iomanip>
using namespace std;
void merge(int, int, int, int*);
void merge_sort(int low, int high, int* p){
int pivot;
static int i(1);
if (high>low)
{
cout << "calling merge_sort: "<<i<<endl; i++;
pivot = low + ((high - low)/2);
cout << pivot << endl;
merge_sort(low, pivot, p);
merge_sort(pivot+1, high, p);
merge(low, pivot, high, p);
}
}
void merge(int l, int pi, int h,int* arr)
{
int start = l;
int mid = pi+1;
while((start<=pi)&&(mid <=h)){
if (arr[start] > arr[mid])
{
int temp = arr[mid];
arr[mid] = arr[start];
arr[start] = temp;
mid++;
}
else
start++;
}
}
int main()
{
int a[] = {2, 42, 3, 7, 1};
merge_sort(0, 4, a);
for (int i = 0; i<=4 ; i++)
cout << a[i] << endl;
return (0);
}
The output is as follows:
calling merge_sort: 1
2
calling merge_sort: 2
1
calling merge_sort: 3
0
calling merge_sort: 4
3
1
3
7
2
42
I have seen some codes for merge sort implementation on stackoverflow but they use another temporary array, which I want to avoid.
Any help is greatly appreciated in sorting this issue.