Merge sort – Python implementation

def Sort(left,right):
    i = j = 0
    result = []
    while i < len(left) and j < len(right):
        if left[i] < right[j]:
            result.append(left[i])
            i = i + 1
        else:
            result.append(right[j])
            j = j + 1
    result = result + left[i:]
    result = result + right[j:]
    return result
def mergeSort(array):
    if len(array) < 2: return array
    else:
        mid = len(array) / 2
        left = mergeSort(array[:mid])
        right = mergeSort(array[mid:])
        return Sort(left,right)
a = list(xrange(10000))
a.reverse()
mergeSort(a)
Advertisements

Tags: , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: