Algorithms with Python Part 2
Algorithms with Python Part 2 Algorithms with Python - Part 2 Refer this article for Algorithms with Python - Part 1. Merge Sort Merge Sort is a divide-and-conquer algorithm that divides a data structure into smaller sub-collections, sorts them, and then merges the sorted sub-parts to produce a final sorted data structure. Merge sort can be implemented using recursion . Let’s implement a merge sort algorithm to sort an unsorted list of square numbers. We have defined the merge_sort function to take a data structure ( list ) as its parameter. We use this function recursively to split the left half and the right half of the list until a list reaches the base case of len(list) > 1 . Merging is done with the help of three while loops. Firstly, we split the original list into two sub lists ( left_half and right_half ) from the first element to the mid element ( left_half ) and from the mid element to the last element ( right_half ). Then we make recursi...