×

Merge Sort using Python

Merge Sort is a technique that is used for sorting elements in an array using a special method known as divide and conquer. It is the best example of the application of divide and conquers. It is one of the most popular sorting algorithms. In this sorting technique, the array is divided into sub-arrays. Each subarray is then sorted individually. After sorting all the sub-arrays, we will combine them to form a sorted array. It is based on the principle of dividing the array into two halves and then again dividing these two sub-halves. We will repeat these processes until we don’t get an array of size 1. Then we will sort these sub-arrays and combine them. We will combine all the sub-arrays until we don’t get a sorted array of the same size as the initial array.

Merge sort is one of the most efficient sorting techniques to sort the data in a small amount of time. While the other sorting algorithm like selection sort, insertion sort, and bubble sort takes O(n2) time to sort the data in the worst-case scenario, merge sort only takes O(nlogn) time to sort the data.

Divide and Conquer Strategy

With the help of this strategy, we can divide a problem into sub-problems. We solve each sub-problem one by one. Once we have the solution to each sub-problems, then we can combine these sub-problems to form the final solution. This is how we solve the main problem using divide and conquer.

For example, if we have to sort an array. Then a sub-problem would be to sort a sub-array section of the array, starting at index m and ending at index r. This sub-array can be denoted as arr[m..r].

Divide

The middle point of the array between m and r is p. We can divide the array into sub-array with the sizes arr[m..p] and arr[p+1..r].

Conquer

In the step, we will sort both arrays and then combine them to form the solution of the actual array. But if we still haven’t reached the base case, we will divide these sub-arrays and try to sort them.

Combine

When we have solved all the sub-problem, then we will use this step to combine them and get the solution to the main problem. In the case of arrays, once we have sorted all the sub-arrays, then we will combine those sub-arrays to get the final sorted array. When we reach the base case, we get two sorted sub-arrays: arr[m..p] and arr[p+1..q]. We will combine these to get a sorted array as arr[m..q].

Merge Sort Algorithm

In this algorithm, the function divides the array into halves repeatedly until we get to the stage where we can perform the merge sort on a subarray of size 1. The condition for that will be when m==p. After that, the merge function will combine the sorted arrays into a larger array until the whole array is combined or merged.

Merge sort is a recursive algorithm. It means that it repeats the process of dividing the array into sub-arrays until we get a base case. All recursive algorithms are dependent on the base case. It also needs to have the ability to combine those base cases. The merge sort is exactly the same. The most important step in this whole process is the merge step. Because if we don’t do it correctly, we will get an array that might be unsorted or partially sorted. The merge step in the merge sort algorithm is responsible for merging two subarrays or two small arrays which are already sorted. After combining these sorted sub-arrays, we will get another array that will be sorted.

Code for Merge Sort Using python

# MergeSort Algorithm in Python
def merge_Sort(array):
    if len(array) > 1:


        #  x is the mid-point from where the array is divided into two subarrays
        x = len(array)//2
        P = array[:x]
        Q = array[x:]


       # Sorting the two halves of the array
        merge_Sort(P)
        merge_Sort(Q)


        i = j = k = 0
        while i < len(P) and j < len(Q):
            if P[i] < Q[j]:
                array[k] = P[i]
                i += 1
            else:
                array[k] = Q[j]
                j += 1
            k += 1


        while i < len(P):
            array[k] = P[i]
            i += 1
            k += 1


        while j < len(Q):
            array[k] = Q[j]
            j += 1
            k += 1




# Printing the sorted array
def printList(list):
    for i in range(len(list)):
        print(list[i], end=" ")
    print()


if __name__ == '__main__':
    list = [11,90,76,5,34,7,98,66]


    merge_Sort(list)


    print("Sorted array using merge sort is: ")
    printList(list)

Output

Sorted array using merge sort is: 
5 7 11 34 66 76 90 98

Explanation

In the above code, we have used a function merge_Sort to sort the array with random elements. Inside that function, we have used three variables: x, P, and Q. x is the mid-point between the first and last index. P and Q are used to copy the two divided sub-arrays. After that, we recursively solved those sub-arrays. Once we reach the base case, we have used three more variables: i, j, and k. These variables are used to merge the sorted sub-arrays. We will compare the subarrays. The array with a smaller number at the given index will be placed in the index k of the new sorted array. We will increment the value of k after each comparison. And based on which condition gets satisfied, we will increment the value of i and j.  We have used three while loops for each condition. One while loop is used to check if the variable i and j are within the array size limits. Another while loop is used when the length of P is larger than Q. Last while loop is used when the size of Q is larger than P. Lastly, we have printed the value of the sorted array list.

Time Complexity

The best case Complexity of Merge Sort is O(n*logn).

The worst Case Complexity of Merge Sort is O(n*logn).

The average Case Complexity of Merge Sort is O(n*logn).

The space Complexity of Merge Sort is O(n).

Advantages Of Merge Sort

  • Merge sort is used to sort data with a large number of elements.
  • Merge sort can be used to access the data in a sequential manner. Hence we don’t need the use for random access.
  • Merge sort is an example of a stable sorting algorithm.
  • Merge sort has the same time complexity for best case, worst case, and average case.

Disadvantages Of Merge Sort

  • It requires an array of the same size as the original array to store the final sorted array, which needs extra space. Hence its space complexity is O(n).
  • It is not efficient when it comes to sorting data sets with small sizes.

Applications

  • Merge sort is used for e-commerce applications.
  • Merge sort is used for both internal and external sorting.
  • It is used in organizing MP3 libraries.
  • It is also used to display Google PageRank results.
  • Merge sort is used in various problems like inversion count problems.

Conclusion

Merge sort has a very important role in today’s applications. For a large amount of data set present on the internet, merge sort can easily and efficiently sort the data set. It is used as a basic strategy for all practical applications in day-to-day life. Hence in this lecture, we have mentioned the algorithm along with its advantage and disadvantage.


Related Topics

Compound Interest GUI Calculator using Tkinter in Python

GUI: One of the most significant factors that increased the usability of computer and digital technologies for common, less tech-savvy users is likely the development and widespread adoption of GUIs. GUIs...

6 minutes read.

Python isinstance() function

Python isinstance() function The isinstance() function in Python returns a Boolean value ‘True’ if the given object is of the specified type, otherwise it returns False. Syntax isinstance(object, classinfo) Parameter object: It is a required parameter which represents an object. classinfo: This...

1 minute read.

Binary Search Visualization using Pygame in Python

A calculation like Binary Search is seen effectively by picturing. Here in this tutorial, a function that pictures the Binary Search Algorithm is carried out. The Graphical User’s Interface (GUI)...

15 minutes read.

Python Letter to Number

Python Letter to Number In this tutorial, we will convert the given letters into numbers using a Python. We will convert the given letter into the letter value as defined in...

3 minutes read.

Python BS4 Code

Python BS4 Code The BS4 stands for BeautifulSoup version 4.x. The BeautifulSoup is a Python library which is used for pulling out data of the HTML & XML files using the...

14 minutes read.

XXhash Python

Python: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said...

6 minutes read.

Python Boolean

In this article, you will learn the boolean variables in python, bool() function in python, and bool operators with examples, Boolean Objects in Python. There are the only two possible values...

6 minutes read.

How to run a Python file in CMD

Introduction Today, let us learn about how to run a Python file using cmd. Cmd is nothing but command prompt. So first let us know how to run a Python code...

4 minutes read.

Difference between Expression and Statement in Python

What is an expression in Python? Expression is a combination of operands and operators. Expression helps us to produce some other values. In the python programming language,  expressions produce some other value...

6 minutes read.

Not supported between instances of str and int in python

In this article, you are going to learn why the type error occurs between instances of string and integer datatypes. Also, you will know what kind of error is the...

6 minutes read.

Python print() function

Python print() function The print() function prints the specified message to the screen or other standard output devices. Syntax print(*objects,sep=' ',end='\n',file=sys.stdout,flush=False) Parameter objects: This function represents an object, which will be converted to a string...

1 minute read.

Python Modules

Python Modules The Python module is a collection of definition and statements. It can contain functions, classes, and variables.  Combining the related code into a module makes the code easier to understand and use....

5 minutes read.

Python Line Break

Introduction In this tutorial, you will learn line breaks in python.In Python, the new line character is used to indicate the start of a new line and the end of an...

5 minutes read.

Math Module in Python

In this article, you are going to learn everything in detail about the “ math “ module in Python. We can normally work with general operations using python without importing or...

16 minutes read.

Python Sending Email

Python Sending Email Simple Mail Transfer Protocol (SMTP) is used to handle sending e-mail and routing e-mail between mail servers. When we send an email either form a web-application or from a local software...

3 minutes read.

Python Project Ideas for Beginners

Python project ideas for beginners Advanced Python is an amazing platform to grow and achieve success as a developer in the future. Python is a programming language that can be very...

7 minutes read.

Text detection using Tkinter in Python

Tkinter: The standard Python technique for building Graphical User Interfaces (GUIs) is Tkinter, which is included in all popular Python distributions. The only framework included in the Python standard library is...

4 minutes read.

Python Tricks: The Book

A Buffet of Awesome Python Features Dan Bader is the author of the book called Python Tricks . he is the owner and editor of Real Python and one of the...

3 minutes read.

Python random.seed() function

The random module in Python produces a random number or pseudo-random data, that is, deterministic. The seed function records the state of a random function to provide the same random...

6 minutes read.

Python Ways to find nth occurrence of substring in a string

Introduction In Python, a string is a collection of characters that can be employed to conduct additional operations. In Python, a substring is a group of characters that are a part...

4 minutes read.