my code stock.com

A program of merge sort during the online edx python course
import operator #indicate that operator class is imported
while operator.lt represent indicate less than comparison

Snippet options

Download: Download snippet as merge-sort.py.
Copy snippet: For this you need a free my code stock.com account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!

import operator
compare = operator.lt
def merge(left, right,compare):
    result = []
    l1 = len(left)
    l2 = len(right)
    i,j = 0,0
    while i<len(left) and j<len(right):
        if compare(right[j],left[i]):
            result.append(right[j])
            j+=1
        else:
            result.append(left[i])
            i+=1
    while(i<len(left)):
        result.append(left[i])
        i+=1
    while(j<len(right)):
        result.append(right[j])
        j+=1
    return result
    
    
def mergeSort(L, compare):
    if len(L)<2:
        return L[:]
    else:
        middle = len(L)/2
        left = mergeSort(L[0:middle],compare)
        right = mergeSort(L[middle:],compare)
        return merge(left,right,compare)

Create a free my code stock.com account now.

my code stok.com is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!

Find out more and register now

You can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.