0

I wrote a Merge sort code, which works fine in my pc but give "Time Limit Exceeded" error on platforms like leetcode. I have heard that merge sort is faster than Selection, Insertion or Bubble sort, yet, it exceeds time limit while other sorts don't at the same platforms. Here is the code:

#include<iostream>
#include<vector>
using namespace std;

    void Merge(vector<int>& nums, int s, int e)
    {
        int mid = (s+e)/2;
        int i=s,j=mid+1, MainIndex=s;

        vector<int>Merge;


        while(i<=mid && j<=e)                          
        {                                              
            if(nums[i]<nums[j])                        
            Merge.push_back(nums[i++]);                

            else if(nums[i]>nums[j])
            Merge.push_back(nums[j++]);
        }

        while(j<=e)
        Merge.push_back(nums[j++]);

        while(i<=mid)
        Merge.push_back(nums[i++]);


        for (int i = 0; i < e-s+1; ++i)
        nums[MainIndex++] = Merge[i];
    }

    void MergeSort(vector<int>& nums, int s, int e)
    {

        if(s>=e)
        return;

        int mid = (s+e)/2;

        MergeSort(nums,s,mid);
        MergeSort(nums,mid+1,e);

        Merge(nums,s,e);

    }

    vector<int> sortArray(vector<int>& nums)
    {
        MergeSort(nums,0,nums.size()-1);
        return nums;
    }





int main()
{
    vector<int> v = {4,1,3,7,5,9,2,6,8,0};

    cout<<"Before: ";
    for (int i = 0; i < v.size(); ++i)
    cout<<v[i]<<" ";

    v = sortArray(v);

    cout<<"\nAfter: ";
    for (int i = 0; i < v.size(); ++i)
    cout<<v[i]<<" ";
}

Am I doing something wrong with the code? Is there something more to be optimized?

  • This is just a subjective opinion, but I'd always use brackets `{ }` for all block-scoped statements no matter whether or not there's just one single line of code within. – Stefan Falk Apr 15 '23 at 15:46
  • @StefanFalk: Let's provide material help before nitpicking style. The choice to not use `{ }` after `if` is a valid choice, it's not wrong, and a lot of people program that way. – Dietrich Epp Apr 15 '23 at 15:49
  • 1
    @DietrichEpp it's well meant advice. It's more readable - even on SO - but first and foremost people who do not bracket their statements have a special place in programming-hell reserved for them :) – Stefan Falk Apr 15 '23 at 15:51
  • @StefanFalk: Are you saying that I belong in hell? – Dietrich Epp Apr 15 '23 at 15:51
  • @DietrichEpp Nooo... the programming god did :) – Stefan Falk Apr 15 '23 at 15:52
  • @StefanFalk: That's why your criticisms are inappropriate. – Dietrich Epp Apr 15 '23 at 15:54
  • @DietrichEpp I was just joking but now you're discriminating against my religious beliefs. (still joking btw) – Stefan Falk Apr 15 '23 at 15:54
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/253166/discussion-between-dietrich-epp-and-stefan-falk). – Dietrich Epp Apr 15 '23 at 15:55

1 Answers1

1

The case nums[i] == nums[j] is not handled, which causes an infinite loop.

if (nums[i] < nums[j])                        
  Merge.push_back(nums[i++]);                
else if (nums[i] > nums[j])
  Merge.push_back(nums[j++]);

Remove the second if:

if (nums[i] < nums[j])                        
  Merge.push_back(nums[i++]);                
else
  Merge.push_back(nums[j++]);
Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415