#pythonintelugu #pythonalgorithms #naveenv
Algorithms in Python | Merge Sort | Full Code with Time Complexity explanation| Telugu
Time Code:
00:00 Intro Animation
02:20 Merge Sort Wiki Eg
04:45 Pseudocode Merge Sort
06:10 Python Coding
14:25 Result Execution
15:00 RECAP
20:00 Outro
MERGE SORT
1. Divide the unsorted list into n sublists,
each containing one element (a list of one element is considered sorted).
2. Repeatedly merge sublists to produce new sorted sublists
until there is only one sublist remaining.
This will be the sorted list.
https://en.wikipedia.org/wiki/Merge_sort
Code:
Note: Replace GT with greather than symbol and LT with less than symbol below
def mergeSort(nums):
if len(nums) GT 1:
mid = len(nums) // 2
left = nums[:mid]
right = nums[mid:]
mergeSort(left)
mergeSort(right)
i = j = k = 0
while i LT len(left) and j LT len(right):
if left[i] LT right[j]:
nums[k] = left[i]
i += 1
else:
nums[k] = right[j]
j += 1
k += 1
while i LT len(left):
nums[k] = left[i]
i += 1
k += 1
while j LT len(right):
nums[k] = right[j]
j += 1
k += 1
return nums
nums = [8, 3, 6, 5, 2, 9]
print(mergeSort(nums))
O (n log n) - Time complexity is the computational complexity that describes the amount of computer time it takes to run an algorithm. Time complexity is commonly estimated by counting the number of elementary operations performed by the algorithm
Time Complexity is commonly expressed using big O notation, typically O(n), O (logn), O(n^2), O(2^n), O(n!) etc where n is the input size in units of bits needed to represent the input.
If you like this content, please like, share and subscribe. Motivates me to create more such content.