Posts: 39
Threads: 22
Joined: May 2018
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?
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)
Posts: 1,032
Threads: 16
Joined: Dec 2016
you change the contents of aList in yourloop.
do you want to sort?
aList = [3, 2, 5, 7, 6, 8]
aList.sort()
print (aList[:len(aList)-2])
Posts: 1,358
Threads: 2
Joined: May 2019
Nov-04-2019, 08:34 PM
(This post was last modified: Nov-04-2019, 08:34 PM by jefsummers.)
In your first for loop, i is assigned the value of each of the elements in the list. So, first time through i = 3. Second time through i = 2. i contains the value, not the location, so first time through i = 3 and i+1 = 4.
If you want to reference the value past "3" first time through you would refer to aList[i+1] which would be 2.
So, why is stop = len(aList) - 2? The length of aList is 6 items, and references to lists are zero based. So, the list consists of aList[0] through aList[5]. Looking ahead, in the second loop you compare aList[i] to aList[i+1]. To avoid the error of going beyond the end of the list, you need to stop the loop at 4, which compares aList[4] and aList[5]. Going on to aList[6] is right out.
Also, you need to fix your indentation. It matters in Python. Add indentation to lines 7 through 9.
And, you are using nested loops with the same variable - i - lines 3 and 5. Big no-no. Change one of them. It is also recommended to use more descriptive variable names
Clear?
Posts: 39
Threads: 22
Joined: May 2018
Thank you so much for clearing on how "i" is used in this context. My initial intuition was right about "i", but then I over think the problem, and started assuming "i+1" was the adjacent value in the list. Okay, now I understand what "len(aList)-" means. Thanks for the clarification!
Posts: 39
Threads: 22
Joined: May 2018
Hi I'm still confuse by this code. In the for loop where, i=3, aList[3] = aList[3+1], is 4 temporarily replacing 3? I'm confuse by this "place holder" substitution. How does this code relate to switching the elements order in this list?
Is aList[3+1] = temp saying 4 is now the temporary value? What does this code actually do; aList=[i+1] = temp?
Posts: 1,358
Threads: 2
Joined: May 2019
You are swapping values.
temp = aList[3] #for example, save the value in the 4th position of aList in a temporary variable
aList[3] = aList[3+1] #move the value in the 5th position to the 4th position
alist[3+1] = temp #now put the value that was in the 4th position into the 5th position.
It's called a bubble sort. Here are some related links. The Wikipedia entry is perhaps a tad complex.
https://www.geeksforgeeks.org/bubble-sort/
https://en.wikipedia.org/wiki/Bubble_sort
Posts: 39
Threads: 22
Joined: May 2018
Right so my question is why does "temp = aList[3]" automatically moves it into the 4th position temporarily? Why not the second position?
Posts: 4,220
Threads: 97
Joined: Sep 2016
It doesn't move it into the 4th position. It moves what is in the 4th position into a temporary variable named 'temp'. You are putting it there to save it while you overwrite what is in the 4th position with what is in the 5th position, which you do on the next line.
Posts: 39
Threads: 22
Joined: May 2018
So now I'm confuse because I though bubble sort is where you compare ADJACENT elements in a list to see which one is greater, and then swap with each other depending on the two value.
So in this case it's; [3, 2, 5, 7, 6, 8]. So is 7, being the 4th position element in this list, is being temporarily store in the variable "temp", i.e. "aList[i+1] = temp", and 3 is being placed in the 4th position? Based on what you're saying the list would look like aList = [2, 5, 3, 7, 6, 8]. Is this what you're saying?
Posts: 1,358
Threads: 2
Joined: May 2019
if aList[i] > aList[i+1]: That is a comparison of adjacent elements in the list.
First comparison is 3 vs 2. Swaps them and you have [2,3,5,7,6,8]. Next comparison is 3 vs 5. OK, continue. 5 vs 7. OK, continue. 7 vs 6. Swap, so now 2,3,5,6,7,8. All subsequent comparisons do not result in a swap.
|