0

Can anyone please explain why my Merge Sort code is not working; CPP; I used very Normal Approach but still its not working properly.

#include<iostream>
    using namespace std;

void merge(int a[], int l, int mid, int r)
{   int p[r]; 
    int i=l;
    int j= mid+1;
    int k=l;
    while(i<=mid && j<=r)
    {
        if(a[i]<a[j])
        {
            p[k] = a[i];
            i++;
        }
        else
        {
            p[k] = a[j];
            j++;    
        }
        k++;    
    }
    for(k=l; k<=r; k++)
    {
        a[k]=p[k];
    }
}

void ms(int a[], int l, int r)
{
    if(l<r)
    {
        int mid = (l+r)/2;
        ms(a,l,mid);
        ms(a, mid+1, r);
        merge(a,l,mid,r);
    }
}

int main()
{
    int a[]={1,9,4,6,2,7};
    int l=0; int r=5;
    ms(a,l,r);
    for(int i=0; i<=r; i++)
    {
        cout<<a[i];
    }
    return 0;
}

I learned this code from a youtube tutorial: https://www.youtube.com/watch?v=aDX3MFL0tYs

Its output should be 1,2,4,6,7,9 but it is showing 102700, please help.

Sachin
  • 11
  • 1
    `int p[r];` is not standard c++. [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) – 463035818_is_not_an_ai Jul 04 '21 at 17:23
  • 2
    Please edit your post with the text results of your debugging session. Which statement is causing the issue? Are the values in variables correct? – Thomas Matthews Jul 04 '21 at 17:25
  • How does your *Normal* merge sort differ from other merge sorts on the internet? – Thomas Matthews Jul 04 '21 at 17:26
  • `int p[r]; ` --> `std::vector p(r);` – PaulMcKenzie Jul 04 '21 at 17:33
  • `p[k] = a[i];` -- Print out the values that you will be populating the `a` array with. It is obvious that you've messed up the `a` array with values from `p` here: `for(k=l; k<=r; k++) { a[k]=p[k]; }`, and the only place `p` gets its values is from the original `a` array. – PaulMcKenzie Jul 04 '21 at 17:42
  • As to your question: *Can anyone please explain why my Merge Sort code is not working; CPP; Normal Approach* -- You're supposed to know why the code is not working. You do this by debugging your code and seeing where the problem is originating. Now, *fixing* the problem is a different story, where you point out to us where the problem is, what you're seeing, and you need some way to circumvent the problem (if you haven't found a way). But at the very least, it should be *your* job to see what has gone wrong, and *then* ask for help about solving the problem. – PaulMcKenzie Jul 04 '21 at 17:45
  • Also see [how to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – PaulMcKenzie Jul 04 '21 at 17:47
  • Hi @PaulMcKenzie Thx for repyling, problem with code is that it is showing incorrect output. there is no as such error prompt. – Sachin Jul 04 '21 at 17:48
  • @Sachin -- We know that the program is showing the wrong output. Every programmer in the world, from the beginner to the expert, has written programs that have bugs. What we're asking is for you to debug your code. Debugging code is part and parcel of learning how to write programs. – PaulMcKenzie Jul 04 '21 at 17:54
  • @PaulMcKenzie youre right, thankyou. – Sachin Jul 04 '21 at 18:04
  • Hint: consider when your `while` loop might exit before you think it should. – Welbog Jul 04 '21 at 18:16

0 Answers0