-4

Somewhere in my program there is loop or declaration problem kindly anyone bug the error in the following program.And say whether this algorithm be a good solution for sorting problem.

#include<iostream>
using namespace std;
void merge(int a[],int start,int mid,int end){
  int p=start,q=mid+1;
  int Arr[end-start+1],k=0;
  for(int i=start;i<=end;i++){
    if(p>mid)
    Arr[k++]=a[q++];
    else if(q>end)
    Arr[k++]=a[p++];
    else if(a[p]<a[q])
    Arr[k++]=a[p++];
    else
    Arr[k++]=a[q++];
    for(int i=0;i<k;i++)
    a[start++]=Arr[i];
  }

}
void merge_sort(int a[],int start,int end){
int mid;
  if(start<end){
   int mid= (start+end) /2;
}
  merge_sort(a,start,mid);
  merge_sort(a,mid+1,end);
  merge(a,start,mid,end);
}
int main(){
  int n;
  cout<<"Enter the n:"<<endl;
  cin>>n;
  int a[n];
  cout<<"Enter the values in the array:"<<endl;
  for(int i=0;i<n;i++){
    cin>>a[i];
  }
  int start=0,end=n-1;
  merge_sort(a,start,end);
  cout<<"After Sorting: "<<endl;
  for(int i=0;i<n;i++){
    cout<<a[i]<<" ";
  }
  return 0;
}
Robin Reni
  • 53
  • 5
  • 6
    Welcome to Stack Overflow! It sounds like you may need to learn how to use a [debugger](https://en.wikipedia.org/wiki/Debugger) to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Paul R Nov 10 '17 at 16:11
  • 1
    [`using namespace std;` is a bad practice](https://stackoverflow.com/q/1452721/2176813), never use it. – tambre Nov 10 '17 at 16:16
  • Instead what can I do – Robin Reni Nov 10 '17 at 16:35
  • 1
    Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation. [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. – Prune Nov 10 '17 at 16:36

2 Answers2

0

There is something very wrong with your merge_sort function:

void merge_sort(int a[], int start, int end)
{
  int mid;

  if (start < end)
      mid = (start+end) / 2; // Also, don't re-declare mid with int!

  merge_sort(a, start, mid);
  merge_sort(a, mid + 1, end);
  merge(a, start, mid, end);
}

You're unconditionally calling merge_sort inside of merge_sort. Even if your program didn't crash, it would never complete, and probably be killed by your system because of the mess it's causing.

I suggest you to get more comfortable recursive functions before trying to fix your code.

Nepho
  • 1,074
  • 1
  • 11
  • 32
0

The mistake is the displacement of brackets.

#include<iostream>
using namespace std;
void merge(int a[],int start,int mid,int end){
  int p=start,q=mid+1;
  int Arr[end-start+1],k=0;
  for(int i=start;i<=end;i++){
    if(p>mid)
    Arr[k++]=a[q++];
    else if(q>end)
    Arr[k++]=a[p++];
    else if(a[p]<a[q])
    Arr[k++]=a[p++];
    else
    Arr[k++]=a[q++];
  }
    for(int i=0;i<k;i++)
  {
      a[start++]=Arr[i];
  }

}
void merge_sort(int a[],int start,int end){
  if(start<end){
    int mid= (start+end) /2;
  merge_sort(a,start,mid);
  merge_sort(a,mid+1,end);
  merge(a,start,mid,end);
}
}
int main(){
  int n;
  cout<<"Enter the n:"<<endl;
  cin>>n;
  int a[n];
  cout<<"Enter the values in the array:"<<endl;
  for(int i=0;i<n;i++){
    cin>>a[i];
  }
  int start=0,end=n-1;
  merge_sort(a,start,end);
  cout<<"After Sorting: "<<endl;
  for(int i=0;i<n;i++){
    cout<<a[i]<<" ";
  }
  return 0;
}
Robin Reni
  • 53
  • 5