I found this code online for the selection sort:
def selectionSort(alist):
for fillslot in range(len(alist)-1,0,-1):
positionOfMax=0
for location in range(1,fillslot+1):
if alist[location]>alist[positionOfMax]:
positionOfMax = location
temp = alist[fillslot]
alist[fillslot] = alist[positionOfMax]
alist[positionOfMax] = temp
alist = [54,26,93,17,77,31,44,55,20]
selectionSort(alist)
print(alist)
list length = 9
i dont understand these lines of code:
for fillslot in range(len(alist)-1,0,-1):
and
for location in range(1,fillslot+1):
for fillslot if we go from those ranges it means we are looking at index: 8,7,6,5,4,3,2,1 why do we not look at index 0?
also for the location variable we would be looking at 1,2,3,4,...fillslot again why are we not looking at index 0?
positionOfMaxstarts at 0. No need to check itself.