Algorithm
Algorithm
def bubble_sort(arr):
n = len(arr)
for i in range(n-1):
for j in range(0, n-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1],
arr[j]
return arr
• Create a new empty array, which will be used to store the merged and sorted elements of
the original arrays.
• Initialize two pointers, one for each of the original arrays.
• Compare the elements at the current pointers in each of the original arrays. Whichever
element is smaller, add it to the new array and increment the pointer for that array.
• Repeat step 3 until one of the original arrays is exhausted.
• Once one of the original arrays is exhausted, add the remaining elements from the other
array to the new array.
• Return the new array, which contains the merged and sorted elements of the original arrays.
def insertion_sort(arr):
for i in range(1, len(arr)):
key = arr[i]
j = i-1
while j >= 0 and key < arr[j]:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
return arr
def selection_sort(arr):
for i in range(len(arr)):
# Find the minimum element in the remaining unsorted portion of the array
min_index = i
for j in range(i+1, len(arr)):
if arr[j] < arr[min_index]:
min_index = j
# Swap the minimum element with the current element
arr[i], arr[min_index] = arr[min_index], arr[i]
return arr