Update adaptive_merge_sort.py

This commit is contained in:
ANANT JAIN 2024-10-17 01:25:53 +05:30 committed by GitHub
parent 15578e80c6
commit f331afd6e3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -9,7 +9,6 @@ def adaptive_merge_sort(sequence: list) -> list:
Merging: array[0:1] and array[1:2] Merging: array[0:1] and array[1:2]
After merge: [3, 4] After merge: [3, 4]
Sorting: array[2:3] and array[3:4] Sorting: array[2:3] and array[3:4]
Skipping merge as array[0] <= array[1]
Skipping merge as array[2] <= array[3] Skipping merge as array[2] <= array[3]
Merging: array[0:2] and array[2:4] Merging: array[0:2] and array[2:4]
After merge: [1, 2, 3, 4] After merge: [1, 2, 3, 4]
@ -24,7 +23,6 @@ def adaptive_merge_sort(sequence: list) -> list:
print(f"Sorted sequence: {sequence}") print(f"Sorted sequence: {sequence}")
return sequence return sequence
def adaptive_merge_sort_helper(array: list, aux: list, low: int, high: int) -> None: def adaptive_merge_sort_helper(array: list, aux: list, low: int, high: int) -> None:
""" """
Helper function for Adaptive Merge Sort algorithm. Helper function for Adaptive Merge Sort algorithm.
@ -51,7 +49,6 @@ def adaptive_merge_sort_helper(array: list, aux: list, low: int, high: int) -> N
return return
merge(array, aux, low, mid, high) merge(array, aux, low, mid, high)
def merge(array: list, aux: list, low: int, mid: int, high: int) -> None: def merge(array: list, aux: list, low: int, mid: int, high: int) -> None:
""" """
Merges two sorted subarrays of the main array. Merges two sorted subarrays of the main array.
@ -75,7 +72,7 @@ def merge(array: list, aux: list, low: int, mid: int, high: int) -> None:
else: else:
aux[k] = array[i] aux[k] = array[i]
i += 1 i += 1
array[low : high + 1] = aux[ # Ensure we correctly copy back to the main array
low : high + 1 for k in range(low, high + 1):
] # Update the main array with merged values array[k] = aux[k]
print(f"After merge: {aux[low:high + 1]}") print(f"After merge: {aux[low:high + 1]}")