In: Computer Science
1.)recursive merge sort on a list.(Python)
2.)recursive bubble sort using a list without enumerate() function.(python)
Show Base case, and recursive case.
Thanks for the question.
Below is the code you will be needing Let me know if you have any doubts or if you need anything to change.
Thank You !!
===========================================================================
#1.)recursive merge sort on a list.(Python)
# merging two sorted list
def merge(left, right):
left_idx, right_idx = 0, 0
result = []
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
result += left[left_idx:]
result += right[right_idx:]
return result
def merge_sort(array):
if len(array) <= 1: # base case
return array
# divide array in half and merge sort recursively
half = len(array) // 2
left = merge_sort(array[:half]) # recursive case
right = merge_sort(array[half:]) # recursive case
return merge(left, right)
#2.)recursive bubble sort using a list without enumerate() function.(python)
def bubbleSort(array):
count = 0
for idx in range(len(array)-1):
if array[idx] > array[idx + 1]:
array[idx],array[idx + 1] = array[idx + 1],array[idx]
count += 1
if count == 0:
return array
else:
return bubbleSort(array)
array = [11,15,21,17,31]
print(merge_sort(array))
array = [11,15,21,17,31]
print(bubbleSort(array))

