Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7697795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:05:37+00:00 2026-05-31T22:05:37+00:00

I am learning MIT’s open course 6.046 Introduction to Algorithms in Youtube, and I

  • 0

I am learning MIT’s open course 6.046 “Introduction to Algorithms” in Youtube, and I was trying to implement the merge sort in python.

My code is

def merge(seq_list, start, middle, end):
    left_list = seq_list[start:middle]
    left_list.append(float("inf"))
    right_list = seq_list[middle:end]
    right_list.append(float("inf"))
    i = 0
    j = 0
    for k in range(start, end):
        if left_list[i] < right_list[j]:
            seq_list[k] = left_list[i]
            i = i + 1
        else:
            seq_list[k] = right_list[j]
            j = j + 1


def merge_sort(seq_list, start, end):
    if start < end:
        mid = len(seq_list)/2
        merge_sort(seq_list[0:mid], start, mid)
        merge_sort(seq_list[mid:], mid, end)
        merge(seq_list, start, mid, end)

And the unittest code is

import unittest
from sorting import *

class SortingTest(unittest.TestCase):
    def testMergeSort(self):
        test_list = [3, 4, 8, 0, 6, 7, 4, 2, 1, 9, 4, 5]
        merge_sort(test_list, 0, 9)
        self.assertEqual(test_list, [0, 1, 2, 3, 4, 4, 4, 5, 6, 7, 8, 9])
    def testMerge(self):
        test_list = [13,17,18,9,2,4,5,7,1,2,3,6,0,38,12]
        merge(test_list, 4, 8, 12)
        self.assertEqual(test_list, [13,17,18,9,1,2,2,3,4,5,6,7,0,38,12])


if __name__ == "__main__":
    unittest.main()

The function merge() seems work perfectly, but the merge_sort() function was wrong, and I don’t know what’s going on. The terminal show me:

RuntimeError: maximum recursion depth exceeded

  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T22:05:38+00:00Added an answer on May 31, 2026 at 10:05 pm

    You need to add a base clause when the list is empty or of size 1, other wise you keep “shrinking” an empty list, [and actually stay with the same list].

    EDIT:

    Also, I think it is actually deriving from a different bug: you are using len(seq) some times, and start,end sometimes – you should just stick to one of them.

        mid = len(seq_list)/2
        merge_sort(seq_list[0:mid], start, mid)
        merge_sort(seq_list[mid:], mid, end)
    

    Have a look on the test case [0,1,2,3]

    start = 0, end = 3 -> mid = 2

    Now you recurse with

    mergesort([2,3],2,3) #2 == mid, 3 == end
    

    And later you will set:

    mid = len([2,3])/2 == 1
    

    and try recursing again with

    mergesort([3],1,3)
    

    You will never reach the “stop condition” of start >= end, because end never changes, and is out of the current list’s bounds!

    Another bug:

    merge_sort(seq_list[0:mid], start, mid)
    

    does not do anything on seq_list, it does not change it – it only changes the new list object you passd to the recursion, and thus merge() will also fail.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm learning CS/Python on MIT's Open Courseware. They want me to design a hangman
For learning purposes, I'm trying to make a function using Python that takes in
I'm working on the MIT open courseware for python but have having a hard
I am currently learning Python from MIT OCW courses ? Anyone who have used
Learning some VBA. So far, I've constructed this piece of code which should allow
learning the basics of Python, now ready for a bigger challange... it didn't take
Learning xml, Can anyone help me? I have following XML code: **<book lang=en>name of
Learning CasperJS Trying to understand why the following is not displaying my results in
Learning to use Ruby Threads for transportability of code between different OS platforms. The
learning about loops (still a beginner) in VB.net. I have got the below code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.