Nov-04-2019, 06:39 PM
Hi, I was wondering if someone can help me understand this Python code. If I have a list with a collection of 3, 2, 5, 7, 6, 8 as show below. How is "i" assigned in this unordered list? Say if I am starting from index 0, which is 3, so is i =3 and i+1 = 2? Assuming if this is the case, then wouldn't 3 and 2 would not be switch, since i = 3 > i + 1= 2?
I tried to workout this algorithm by hand, and I'm not getting the same result as the output. The resulting output is an ordered list of 2, 3, 5, 6, 7, 8.
Also, is len(aList)-2 is 3, 2, 5, 7 instead of 3, 2, 5, 7, 6, 8?
I tried to workout this algorithm by hand, and I'm not getting the same result as the output. The resulting output is an ordered list of 2, 3, 5, 6, 7, 8.
Also, is len(aList)-2 is 3, 2, 5, 7 instead of 3, 2, 5, 7, 6, 8?
aList = [3, 2, 5, 7, 6, 8] stop = len(aList) - 2 for i in aList: for i in range (0, stop): if aList[i] > aList[i+1]: temp = aList[i] aList[i] = aList[i+1] aList[i+1] = temp print (aList)