Created
October 8, 2024 06:03
-
-
Save woshichuanqilz/beb1aa18073802b5f99d59462b7eb095 to your computer and use it in GitHub Desktop.
merge sort
This file contains 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 merge_sort(arr): | |
if len(arr) > 1: | |
mid = len(arr) // 2 # Finding the mid of the array | |
L = arr[:mid] # Dividing the elements into 2 halves | |
R = arr[mid:] | |
merge_sort(L) # Sorting the first half | |
merge_sort(R) # Sorting the second half | |
i = j = k = 0 | |
# Copy data to temp arrays L[] and R[] | |
while i < len(L) and j < len(R): | |
if L[i] < R[j]: | |
arr[k] = L[i] | |
i += 1 | |
else: | |
arr[k] = R[j] | |
j += 1 | |
k += 1 | |
# Checking if any element was left | |
while i < len(L): | |
arr[k] = L[i] | |
i += 1 | |
k += 1 | |
while j < len(R): | |
arr[k] = R[j] | |
j += 1 | |
k += 1 | |
# Example usage | |
if __name__ == "__main__": | |
arr = [38, 27, 43, 3, 9, 82, 10] | |
print("Unsorted array:", arr) | |
merge_sort(arr) | |
print("Sorted array:", arr) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment