Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

Python merge sort algorithm

def mergeSort(A):
    if len(A) > 1:
        print('Splitting' , A)
        mid = len(A)//2
        left = A[:mid]
        right = A[mid:]
        mergeSort(left)
        mergeSort(right)
        i = j = k = 0
        while i < len(left) and j < len(right):
            if left[i] < right[j]:
                A[k] = left[i]
                i = i+1
            else:
                A[k] = left[i]
                j = j+1
            k = k+1
        while i < len(left):
            A[k] = left[i]
            i = i+1
            k = k+1
        while j < len(right):
            A[k] = right[j]
            j = j+1
            k = k+1
        print('merging' , A)
        return(A)
Source by www.geeksforgeeks.org #
 
PREVIOUS NEXT
Tagged: #Python #merge #sort #algorithm
ADD COMMENT
Topic
Name
7+6 =