-1

I don't know what is the problem with my code please help.

it's a merge sort code.

#include<bits/stdc++.h>

using namespace std;
void MergeSort(int l, int r);
void Merge(int l1,int r1,int l2,int r2);
int a[100000];
int main ()
{
    int n;cin >> n;
    for(int i=0;i<n;++i)cin >> a[i];
    MergeSort(0,n-1);
    for(int i=0;i<n;++i)cout << a[i] << endl;
}



void MergeSort(int l, int r){
    if(l<=r){
        return;
    }  
    int m=(l+r)/2;
    MergeSort(l,m);
    MergeSort(m+1,r);
    Merge(l,m,m+1,r);
}


int n[1010];
void Merge(int l1,int r1,int l2,int r2){
     int ind=0;int f1=l1;
     while(l1<=r1 && l2<=r2){
         if(a[l1]>=a[l2]){
            n[ind]=a[l1];++l1;
         }
         else{
            n[ind]=a[l2];++l2;
         }
         ++ind;
     }
    while(l1<=r1){
        n[ind]=a[l1];++l1;++ind;
    }
    while(l2<=r2){
        n[ind]=a[l2];++l2;++ind;
    }

    for(int i=f1;i<=r2;++i){
        a[i]=n[i-f1];
    }
}

So I give this to my application for a test case: 1 6 7 4 2

And my programm give me this:

1

6

7

4

2

the same thing as I give the programme please Help!

  • 6
    sometimes you need to use a debugger, but sometimes it already helps to read the code carefully: `if(l<=r){ return; }` :P – 463035818_is_not_an_ai Feb 08 '19 at 13:25
  • 4
    Please read [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and [Why is “using namespace std” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Some programmer dude Feb 08 '19 at 13:26
  • 2
    And please stop going to online judge/competition sites, they will not teache you to do anything useful except program for such sites, with plenty of bad habits (one or two letter undecipherable variable names, no comments, usually undecipherable code). Please [get a couple of good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) to read, or go take a few classes. – Some programmer dude Feb 08 '19 at 13:29
  • 1
    @user463035818 [Rubber duck debugging.](https://en.wikipedia.org/wiki/Rubber_duck_debugging) – Fantastic Mr Fox Feb 08 '19 at 13:33
  • @Someprogrammerdude I strongly disagree that online judge sites won't teach you anything they are very valuable, personally they thought me exactly how important it is to write readable code because I'd get stuck on problems and couldn't debug it because my code was garbage. Though I agree that you should complement it with a book or some classes. – kingW3 Feb 08 '19 at 14:07
  • 1
    @kingW3 They might be good once you're have some intermediate knowledge of programming in the selected language, including the basic algorithms. But not for absolute beginners or learning the basics IMO. – Some programmer dude Feb 08 '19 at 14:10
  • @FantasticMrFox the duck on my desk is much nicer than the one on the wikipedia picture :P – 463035818_is_not_an_ai Feb 08 '19 at 14:14
  • @Someprogrammerdude I totally agree with that. – kingW3 Feb 08 '19 at 14:33

1 Answers1

3

Your merge sort never executes. You are passing ( 0 , n-1 ) to MergeSort but unfortunately you assign

if(l<=r){
        return;
}

which is always true for each time. This should be changed to

if(l>=r){
        return;
    }
Isaiah
  • 685
  • 2
  • 15
  • 28
Shakil
  • 4,520
  • 3
  • 26
  • 36