Archive

Posts Tagged ‘merge sort’

Python: Sorting Algorithm (3. Merge Sort)

June 3, 2021 3 comments

Learning : Python coding, Math, Algorithm,
Subject: Writing Merge Sorting Algorithm in Python

[NOTE: To keep the code as simple as we can, We WILL NOT ADD any user input Varevecations. Assuming that our user will Enter the right inputs.]

Sorting Algorithm is a way to sort a given list/Array of numbers, there are several sorting Algorithm as follow:
Type of Sorting Algorithm
1. Quick Sort. [Click Here]
2. Bubble Sort. [Click Here]
3. Merge Sort. [Click Here]
4. Insertion Sort. [Click Here]
5. Selection Sort. [Click Here]
6. Heap Sort. [Click Here]
7. Radix Sort. [Click Here]
8. Bucket Sort. [Click Here]

Here in this post we will write a function to take a given list and sort it then pass it back. We assume the user will enter a serial of numbers, that he want to sort, our function will sort it and print out the original numbers and the sorted one.

Merge Sort: Steps of Merge Sorting Algorithm are:

1. If the Array has 1 Element then return it.
2. If the givin Array has more than 1 Element then we Divide it into 2 Parts.
Left = from Element 0 T0 Element len(arr)//2
and Right = from Element len(arr)//2 To Element len(arr). And we keep repeating step 2 for each part.
3. Once we have All parts divided we tart merge them in a sorted order.

Coding: In this Post we will write the Function for Main-Mnu, another one to collect the Array [Not Sorted] Elements from the user, then we will write the Main Function, here we will have One to Divide the Array into parts and One to Merge the Element together again. And as we done with Quick & Bubble Sort projects we will have one Fast-Run Function and another one with Details Step-By-Step run to show the Algorithm.

NOTE: All the Code will be Available in the Download Page.

Let’s start with the Main-menu to list down three option were the user will select one among them, the menu options are: 1. Merge Sort Algorithm – Fast Run. & 2. Merge Sort Algorithm – Step By Step and the last one is 3. Exit.
Here is the code.

python code project main menu by Ali Radwani merge sort Algorithm

I will Not post the code for collecting the Array Element from the user [Find it in the .py file for this project]. Here is the code for merge_sort_divider Function, it takes two arguments the Array and the user_selection, so if the user selection is 2 (2. Merge Sort Algorithm – Step By Step) We will print-out the Dividing Steps and some other Text. [We are using If Statement to print-out Text lines]. Here is the Code ..

ali radwani python code merge sort algorithm



Now for merging function, for merging function we will have two copies, one as Fast-Run and another for Details to print-out the running Steps, this will make the Fast-Run Function short and easy to follow-up. So, here is the Fast-Run Function code for merge the two part of array returned from the merge_sort_divider. Here is the Code ..

Run time screen ..

ali radwani python code merge sort algorithm



End of Merge Sorting Algorithm.

To Download my Python code (.py) files Click-Here



Follow me on Twitter..

By: Ali Radwani