Проблема в этом утверждении
while listToBeSorted[currentIndex] > itemToBeInserted and currentIndex > 0
, которое должно быть
while listToBeSorted[currentIndex] > itemToBeInserted and currentIndex > -1
Если currentIndex
всегда больше 0, то первый элемент списка никогда не сортируется, потому чтони один элемент в списке не будет вставлен в начало списка.
listToBeSorted = [7,2,4,3,6,5,1]
for pointer in range(1, len(listToBeSorted )):
itemToBeInserted = listToBeSorted[pointer]
currentIndex = pointer - 1
while listToBeSorted[currentIndex] > itemToBeInserted and currentIndex > -1:
listToBeSorted[currentIndex + 1] = listToBeSorted[currentIndex]
currentIndex -= 1
listToBeSorted[currentIndex + 1] = itemToBeInserted
print(listToBeSorted)