Created
August 30, 2020 09:26
-
-
Save tiwarinaman/ae65557866635a86b76bce65d4a83941 to your computer and use it in GitHub Desktop.
Merge Sort in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def mearge_sort(list): | |
| if len(list) > 1: | |
| mid = len(list)//2 | |
| left_half = list[:mid] | |
| right_half = list[mid:] | |
| mearge_sort(left_half) | |
| mearge_sort(right_half) | |
| i=j=k=0 | |
| while i < len(left_half) and j < len(right_half): | |
| if left_half[i] < right_half[j]: | |
| list[k] = left_half[i] | |
| i+=1 | |
| else: | |
| list[k] = right_half[j] | |
| j+=1 | |
| k+=1 | |
| while i<len(left_half): | |
| list[k] = left_half[i] | |
| i+=1 | |
| k+=1 | |
| while j<len(right_half): | |
| list[k] = right_half[j] | |
| j+=1 | |
| k+1 | |
| list = [32,21,0,9,5,34] | |
| mearge_sort(list) | |
| print(list) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment