Implement Merge Sort using Python

If you’re in a computer science background, widening your technical horizons is the best thing you should do. However, your programming skills matter the most and one must continuously keep sharpening these skills. And to become a better programmer in the future. Though there are multiple things to focus on, one specific area you must focus on is the world of data structures. Data structures in general are specific approaches to solve problems in such a way that computer resources get used minimum. In general, there are multiple data structures you can learn and implement as well. However, to keep things simple were going to start with some mediocre programs you must learn before moving on to complex algorithms. Therefore today we’re going to learn how to Implement Binary Merge Sort using python.

 

What is Merge Sort?

 

  • One of the very efficient sorting algorithms in data structures. Merge Sort is the way of arranging elements in a sequence or ascending order.

 

  • Merge sort follows the principles of the divide & conquer Algorithm. The base array is divided into two arrays. And is further divided till the arrangement of elements doesn’t form a sequence.

 

Also Read: Implement Binary Insertion Sort using Python

 

What’s The Approach?

 

  • Firstly we’ll find the midpoint of the array so that we know from where to divide the array.

 

  • So let us consider the array, array[begin..mid..end] .So the first subarray will be L[begin..mid] and the second subarray will be R[mid+1..end]

 

  • The above instructions will be executed recursively till there’s only a single element in the subarray.

 

  • Once the elements are sorted we’ll merge both the subarrays in ascending order.

 

  • The above three instructions will continue to repeat till the entire array is sorted.

 

Python Program To Implement Merge Sort

 

Input:

 

12, 11, 13, 5, 6, 7

 

Output:

 

Sorted array is: 

 

5 6 7 11 12 13

 

 

# Python program for implementation of MergeSort
def mergeSort(arr):
    if len(arr) > 1:

        # Finding the mid of the array
        mid = len(arr)//2

        # Dividing the array elements
        L = arr[:mid]

        # into 2 halves
        R = arr[mid:]

        # Sorting the first half
        mergeSort(L)

        # Sorting the second half
        mergeSort(R)

        i = j = k = 0

        # Copy data to temp arrays L[] and R[]
        while i < len(L) and j < len(R):
            if L[i] < R[j]:
                arr[k] = L[i]
                i += 1
            else:
                arr[k] = R[j]
                j += 1
            k += 1

        # Checking if any element was left
        while i < len(L):
            arr[k] = L[i]
            i += 1
            k += 1

        while j < len(R):
            arr[k] = R[j]
            j += 1
            k += 1

# Code to print the list


def printList(arr):
    for i in range(len(arr)):
        print(arr[i], end=" ")
    print()


# Driver Code
if __name__ == '__main__':
    arr = [12, 11, 13, 5, 6, 7]
    print("Given array is", end="\n")
    printList(arr)
    mergeSort(arr)
    print("Sorted array is: ", end="\n")
    printList(arr)

 

 

 

Ethix

I'm a coding geek interested in cyberspace who loves to write and read

Leave a Reply

Your email address will not be published. Required fields are marked *