Я хотел знать, может ли кто-нибудь мне помочь? У меня есть простой код из 2/3 строк для вызова другой функции через Python, и я не могу в этом разобраться. Исходная быстрая сортировка2 работает, и сортировка вставкой работает, но вызов функции не работает (они находятся в одном каталоге) – кто-нибудь знает, что делать? Спасибо. Я опубликую оба кода ниже.
В задании конкретно указано: когда n ≤ 16, Quicksort2 не будет секционировать список, а вместо этого вызовет сортировку вставками. Это означает, что мне нужен простой оператор if-elif в моей функции быстрой сортировки 2, но я не могу понять, что делать.
# insertion sort function for an array
def insertion_sort(array_values):
for i in range(1, len(array_values)):
# condition for previous index greater than current index
while i > 0 and array_values > array_values:
# setting our previous index as current index, and current index as previous index
array_values, array_values = array_values, array_values
# decrementing index at i for index pointer
i -= 1
# testing our insertion sort with a given array of values
array_values = [38,89,27,77,16,86,29,20,1,7]
insertion_sort(array_values)
print(array_values)
# ******************************************
def quicksort2(array, low, high):
# THIS PART NEEDS TO CALL MY INSERTION SORT
#if len(array) low:
index = partition(array, low, high)
quicksort2(array, low, index - 1)
quicksort2(array, index + 1, high)
def partition(array, low, high):
firstitem = array[low]
j = low
for i in range(low+1, high+1):
if array < firstitem:
j+=1
array[j], array = array, array[j]
index = j
array[low], array[index] = array[index], array[low]
return index
array = [10, 3, 4, 8, 1, 7, 0, 13]
quicksort2(array, 0, len(array)-1)
for j in range(len(array)):
print ("%d" %array[j])