We will look at the Merge Sort Algorithm which is used to sort an unsorted list. The input list is divided into two parts and they are solved recursively and then they are merged.
Worst Case Time Complexity: O(n log n)
Worst Case Space Complexity: O(n)
Merge sort is a divide and conquer algorithm.
1. We are given an unsorted list of n numbers
2. Divide it into two array of equal size
3. Again Divide those two into another two array of equal size
4. Keep on dividing it until one element is left in each array.
5. Keep Merging the sub arrays to produce sorted arrays until there is only one array remaining.. This will be our sorted array.
while(i < len(left) and j < len(right)):
if(left[i] < right[j]):
while(i < len(left)):
while(j < len(right)):
left =  right =  n = len(A)
mid = n/2
for i in range(mid):
for i in range(mid,n):
A = [2,4,1,3,8,9,0,-2,-1] #given unsorted array
You can take this Python Quiz