Skip to content

Commit

Permalink
Merge Sort in Python
Browse files Browse the repository at this point in the history
  • Loading branch information
Somil-Shukla committed Oct 21, 2023
1 parent d6cc727 commit 0ae3449
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions DSA_Codesheet/Python/merge_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
def merge_sort(arr):
# Base case: If the list has 0 or 1 elements, it is already sorted
if len(arr) <= 1:
return arr

# Divide the input list into two halves
middle = len(arr) // 2
left_half = arr[:middle]
right_half = arr[middle:]

# Recursively sort the two halves
left_half = merge_sort(left_half)
right_half = merge_sort(right_half)

# Merge the sorted halves
return merge(left_half, right_half)

def merge(left, right):
result = []
left_idx, right_idx = 0, 0

# Compare and merge elements from the left and right halves
while left_idx < len(left) and right_idx < len(right):
if left[left_idx] < right[right_idx]:
result.append(left[left_idx])
left_idx += 1
else:
result.append(right[right_idx])
right_idx += 1

# Append any remaining elements from the left and right halves
result.extend(left[left_idx:])
result.extend(right[right_idx:])
return result

# Input: Size of the list
n = int(input())
# Input: List of integers
arr = list(map(int, input().split()))

# Perform Merge Sort
sorted_arr = merge_sort(arr)

# Output: Sorted list of integers
print(" ".join(map(str, sorted_arr)))

0 comments on commit 0ae3449

Please sign in to comment.