I'm trying to implement a hybrid algorithm that combines Insertion Sort and Quick Sort to get the desired features of both in such a way so that when the elements are fewer than 50 in a quicksort step, Insertion Sort is used and for greater than 50 the elements are merged using the Quick Sort algorithm. I attempt to sort the elements to the left of the pivot_index and those to the right of the pivot_index using insertion sort if they are less than 50 by passing a slice instance of the original list as a parameter into my insertionSort function but my list remains the same, partitioned yet unsorted. Any indictors to fix this issue would be deeply appreciated.
from random import randint
def insertionSort(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 = j - 1
arr[j+1] = key
def partition(a, low, high): #lomuto partition scheme
i = low - 1 #directly to the left of our specified low-high range
pivot = a[high] #pivot is the last value in our range, the index equal to high parameter
for j in range(low, high):
if a[j] <= pivot:
i += 1
a[i], a[j] = a[j], a[i]
a[i+1], a[high] = a[high], a[i+1]
return i+1
def quicksort_inplace(a, low=0, high=None):
if high == None:
high = len(a) - 1
if low < high:
pivot_index = partition(a, low, high) #partition around pivot
print("The pivot_index is:", pivot_index)
print("Low is:", low)
print("High is:", high)
if pivot_index - low <= 50:
insertionSort(a[low:pivot_index])
elif high - pivot_index <= 50:
insertionSort(a[pivot_index+1:high])
else:
quicksort_inplace(a, low, pivot_index - 1) #sort lower half
quicksort_inplace(a, pivot_index + 1, high) #sort upper half
def create_arr(size=10):
return [randint(0,50) for _ in range(size)]
arr = create_arr()
print(arr)
quicksort_inplace(arr)
print()
print("The sorted array is:", arr)