Skip to content

Commit

Permalink
Update Merge sort.py
Browse files Browse the repository at this point in the history
  • Loading branch information
JafarBagirov authored Dec 22, 2024
1 parent e61da4e commit 229de21
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions Merge sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,41 @@ def merge(left, right):
lst = [38, 27, 43, 3, 9, 82, 10]
sorted_lst = merge_sort(lst)
print(sorted_lst)

# II method non rekursiv _______________________________________
arr = [38, 27, 43, 3, 9, 82, 10, -8, -89, -56, -75, 46, 443, 4, 3, 4, 3, 4, 95, 6, 11654, 612, 64, 6]

n = len(arr)
hisse = 1
while hisse < n:
for left_sratr in range(0, n, hisse*2):
mid = min(left_sratr+hisse-1, n-1)
right_end = min(left_sratr + hisse*2-1, n-1)
n1 = mid-left_sratr+1
n2 = right_end - mid
L = arr[left_sratr:left_sratr+n1]
R = arr[mid+1:mid+1+n2]
i=j=0
k = left_sratr



while i < n1 and j < n2:
if L[i] < R[j]:
arr[k] = L[i]
i += 1
else:
arr[k] = R[j]
j += 1
k += 1
while i < n1:
arr[k] = L[i]
i += 1
k += 1
while j < n2:
arr[k] = R[j]
j += 1
k += 1
hisse *= 2
print(arr)

0 comments on commit 229de21

Please sign in to comment.