Technology -> Showcase

Python Merge Sort by RoxNoAnne on 2023-03-27 21:19:17


Posted by : RoxNoAnne
Joined on
2023-03-24 17:58:14

showing off my python skillz

def merge(left, right):
    out = []
    while (len(left) > 0 and len(right) > 0):
        if (left[0] <= right[0]):
            out.append(left[0])
            left.remove(left[0])
        else:
            out.append(right[0])
            right.remove(right[0])
    while (len(left) > 0):
        out.append(left[0])
        left.remove(left[0])
    while(len(right) > 0) :
        out.append(right[0])
        right.remove(right[0])
    return out

def merge_sort(arr):
    if (len(arr) > 1):
        midpoint = len(arr)//2
        left = arr[:midpoint]
        right = arr[midpoint:]

        # Use recursive functions to define left and right
        left = merge_sort(left)
        right = merge_sort(right)
        # Use the user-defined merge method to merge the left and right
        return merge(left, right)
    else:
        return arr

super swag
it can sort half a million items in 19-20 seconds