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 9239471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:59:40+00:00 2026-06-18T07:59:40+00:00

I have here a code for sorting lists using merge sort.. I just got

  • 0

I have here a code for sorting lists using merge sort.. I just got it somewhere in the net.. But honestly, I can’t follow the flow of the code… I mean, I can’t get how it is implemented. I can understand some part especially the first one where it divides the entire list in two and then sort each side of the list. And then what?? Will you please tell what’s happening here? Thank you. 🙂

def merge(badlist):
    if len(badlist) == 1:  
    return badlist  
    m = len(badlist)/2  
    l = merge(badlist[:m])  
    r = merge(badlist[m:])  
    if not len(l) or not len(r):  
        return l or r  
    result = []  
    i = j = 0  
    while (len(result) < len(r) + len(l)):  
        if l[i] < r[j]:  
            result.append(l[i])  
            i += 1  
        else:  
            result.append(r[j])  
            j += 1  
        if i == len(l) or j == len(r):  
            result.extend(l[i:] or r[j:]) 
            break  
    return result  
print merge(badlist)  
  • 1 1 Answer
  • 0 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-06-18T07:59:41+00:00Added an answer on June 18, 2026 at 7:59 am

    Going line by line,

    if not len(l) or not len(r):  
            return l or r
    

    This checks whether either of the lists is empty. If it is, then the other list is returned.

    result = []  
    i = j = 0  
    

    Here, result and i,j are initialized

    while (len(result) < len(r) + len(l)):  
    

    Runs the while loop till all elements of both lists have been copied to the result list.

        if l[i] < r[j]:  
                result.append(l[i])  
                i += 1  
        else:  
                result.append(r[j])  
                j += 1  
    

    This block checks which of l[i] or r[j] is lesser and then appends it to result, and advances the respective counter. This goes on till either of the lists gets completely used up, which is checked for in the following code block :

        if i == len(l) or j == len(r):  
                result.extend(l[i:] or r[j:]) 
                break 
    

    Now, whichever list still contains elements, is appended to the end of result list as is.

    return result
    

    After which result is returned.

    As mentioned, the indentation is incorrect in your sample, which I have corrected here.

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

Sidebar

Related Questions

I have some code here which works perfectly in firefox but not in chrome
I have to sort an array here is the code I have. When I
I have trouble sorting two related but separate lists of tuple lists. One list
I have got this large list of names here http://projecteuler.net/project/names.txt , which I am
Using javascript sort() method, I am trying to do sorting a list but sorting
I have some code here that uses bitsets to store many 1 bit values
I have this code here printf '$request1 = select * from whatever where this
I have this code here, which is intended to allow any type of arguments:
enter code here I have a table on SQL server 2005 with bigint primary
I have this code here, {foreach from=$cart.cartItems item=item name=cart} <div id=cart2Produkt> <p>{if $item.Product.ID} <a

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.