There’s a part of the selection sort algorithm that I don’t understand. In the latter part of the code (where the temp variable is used, why are L[i] and L[minIndx] assigned values? Aren’t those values themselves? Can’t only variables be assigned values?
def selSort(L):
for i in range(len(L) - 1):
minIndx = i
minVal = L[i]
j = i+1
while j < len(L):
if minVal > L[j]:
minIndx = j
minVal = L[j]
j += 1
# aren’t L[i] and L[minIndx] values? How can they be assigned to new values?
if minIndx != i:
temp = L[i]
L[i] = L[minIndx]
L[minIndx] = temp
L[0]for example references a value or reference … if you have an arrayL=[10,20,30]…L[0]references the value10and when you doL[0]the result will be the value of the referenced index –10… when you doL[0] = 100… this value for index0will be changed to100. you can not do10 = 100because10is a constant value, not a variable, but you can doL[0] = 100becauseL[0]references a place in the array.