Exercise 1: Reverse List output

I tried to recycle a while loop from a previous assignment and learned that unlike for loops, while loops do not reset the index on each iteration. I guess that's part of the dynamic checking of the conditional statement. Regardless, here's a for loop:

list = [1, 2, 3, 4, 5]

#Index is zero
i = 0
# For loops reset the index on each iteration. I tried a while loop and found that while loops do not reset the index.
# This sets the index to the last element, second to last element, etc. by substracting one and the index from the list's length.
# and making this the new index. It then prints the corresponding element.
for i in range(5):
        i = len(list) - 1 - i
        record = list[i]
        print(record)
5
4
3
2
1

Exercise 2: Bubble Sort

Here's something from Wikipedia (Dec 9 9:42 AM):

Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the input list element by element, comparing the current element with the one after it, swapping their values if needed.

OK, looks like I just need to make a for loop. If a given element is greater than the next element, I swap them (likely using temp variables to catalyze the assignment process)

list = [9, 8, 4, 3, 5, 2, 6, 7, 1, 0]
print(list)

for i in list:
        prev = -1
        for i in list:
                print("element number " +str(list[i]))
                print("This is " +str(i))
                print("Previous is " +str(prev))
                if i < prev:
                        print("Switch triggered")
                        a = list.index(prev)
                        b = list.index(i)
                        print("a is " + str(a))
                        print("b is "+str(b))
                        print(str(list[a]))
                        print(str(list[b]))
                        # idea from https://stackoverflow.com/questions/2493920/how-to-switch-position-of-two-items-in-a-python-list
                        list[a], list[b] = list[b], list[a]
                        print(str(list[b]))
                        print(str(list[a]))
                        print(list)
                        prev = list[b]
                else:
                        prev = i
print("final list is " +str(list))
[9, 8, 4, 3, 5, 2, 6, 7, 1, 0]
element number 0
This is 9
Previous is -1
element number 1
This is 8
Previous is 9
Switch triggered
a is 0
b is 1
9
8
9
8
[8, 9, 4, 3, 5, 2, 6, 7, 1, 0]
element number 5
This is 4
Previous is 9
Switch triggered
a is 1
b is 2
9
4
9
4
[8, 4, 9, 3, 5, 2, 6, 7, 1, 0]
element number 3
This is 3
Previous is 9
Switch triggered
a is 2
b is 3
9
3
9
3
[8, 4, 3, 9, 5, 2, 6, 7, 1, 0]
element number 2
This is 5
Previous is 9
Switch triggered
a is 3
b is 4
9
5
9
5
[8, 4, 3, 5, 9, 2, 6, 7, 1, 0]
element number 3
This is 2
Previous is 9
Switch triggered
a is 4
b is 5
9
2
9
2
[8, 4, 3, 5, 2, 9, 6, 7, 1, 0]
element number 6
This is 6
Previous is 9
Switch triggered
a is 5
b is 6
9
6
9
6
[8, 4, 3, 5, 2, 6, 9, 7, 1, 0]
element number 7
This is 7
Previous is 9
Switch triggered
a is 6
b is 7
9
7
9
7
[8, 4, 3, 5, 2, 6, 7, 9, 1, 0]
element number 4
This is 1
Previous is 9
Switch triggered
a is 7
b is 8
9
1
9
1
[8, 4, 3, 5, 2, 6, 7, 1, 9, 0]
element number 8
This is 0
Previous is 9
Switch triggered
a is 8
b is 9
9
0
9
0
[8, 4, 3, 5, 2, 6, 7, 1, 0, 9]
element number 0
This is 8
Previous is -1
element number 2
This is 4
Previous is 8
Switch triggered
a is 0
b is 1
8
4
8
4
[4, 8, 3, 5, 2, 6, 7, 1, 0, 9]
element number 5
This is 3
Previous is 8
Switch triggered
a is 1
b is 2
8
3
8
3
[4, 3, 8, 5, 2, 6, 7, 1, 0, 9]
element number 6
This is 5
Previous is 8
Switch triggered
a is 2
b is 3
8
5
8
5
[4, 3, 5, 8, 2, 6, 7, 1, 0, 9]
element number 5
This is 2
Previous is 8
Switch triggered
a is 3
b is 4
8
2
8
2
[4, 3, 5, 2, 8, 6, 7, 1, 0, 9]
element number 7
This is 6
Previous is 8
Switch triggered
a is 4
b is 5
8
6
8
6
[4, 3, 5, 2, 6, 8, 7, 1, 0, 9]
element number 1
This is 7
Previous is 8
Switch triggered
a is 5
b is 6
8
7
8
7
[4, 3, 5, 2, 6, 7, 8, 1, 0, 9]
element number 3
This is 1
Previous is 8
Switch triggered
a is 6
b is 7
8
1
8
1
[4, 3, 5, 2, 6, 7, 1, 8, 0, 9]
element number 4
This is 0
Previous is 8
Switch triggered
a is 7
b is 8
8
0
8
0
[4, 3, 5, 2, 6, 7, 1, 0, 8, 9]
element number 9
This is 9
Previous is 8
element number 6
This is 4
Previous is -1
element number 2
This is 3
Previous is 4
Switch triggered
a is 0
b is 1
4
3
4
3
[3, 4, 5, 2, 6, 7, 1, 0, 8, 9]
element number 7
This is 5
Previous is 4
element number 5
This is 2
Previous is 5
Switch triggered
a is 2
b is 3
5
2
5
2
[3, 4, 2, 5, 6, 7, 1, 0, 8, 9]
element number 1
This is 6
Previous is 5
element number 0
This is 7
Previous is 6
element number 4
This is 1
Previous is 7
Switch triggered
a is 5
b is 6
7
1
7
1
[3, 4, 2, 5, 6, 1, 7, 0, 8, 9]
element number 3
This is 0
Previous is 7
Switch triggered
a is 6
b is 7
7
0
7
0
[3, 4, 2, 5, 6, 1, 0, 7, 8, 9]
element number 8
This is 8
Previous is 7
element number 9
This is 9
Previous is 8
element number 5
This is 3
Previous is -1
element number 6
This is 4
Previous is 3
element number 2
This is 2
Previous is 4
Switch triggered
a is 1
b is 2
4
2
4
2
[3, 2, 4, 5, 6, 1, 0, 7, 8, 9]
element number 1
This is 5
Previous is 4
element number 0
This is 6
Previous is 5
element number 2
This is 1
Previous is 6
Switch triggered
a is 4
b is 5
6
1
6
1
[3, 2, 4, 5, 1, 6, 0, 7, 8, 9]
element number 3
This is 0
Previous is 6
Switch triggered
a is 5
b is 6
6
0
6
0
[3, 2, 4, 5, 1, 0, 6, 7, 8, 9]
element number 7
This is 7
Previous is 6
element number 8
This is 8
Previous is 7
element number 9
This is 9
Previous is 8
element number 5
This is 3
Previous is -1
element number 4
This is 2
Previous is 3
Switch triggered
a is 0
b is 1
3
2
3
2
[2, 3, 4, 5, 1, 0, 6, 7, 8, 9]
element number 1
This is 4
Previous is 3
element number 0
This is 5
Previous is 4
element number 3
This is 1
Previous is 5
Switch triggered
a is 3
b is 4
5
1
5
1
[2, 3, 4, 1, 5, 0, 6, 7, 8, 9]
element number 2
This is 0
Previous is 5
Switch triggered
a is 4
b is 5
5
0
5
0
[2, 3, 4, 1, 0, 5, 6, 7, 8, 9]
element number 6
This is 6
Previous is 5
element number 7
This is 7
Previous is 6
element number 8
This is 8
Previous is 7
element number 9
This is 9
Previous is 8
element number 4
This is 2
Previous is -1
element number 1
This is 3
Previous is 2
element number 0
This is 4
Previous is 3
element number 3
This is 1
Previous is 4
Switch triggered
a is 2
b is 3
4
1
4
1
[2, 3, 1, 4, 0, 5, 6, 7, 8, 9]
element number 2
This is 0
Previous is 4
Switch triggered
a is 3
b is 4
4
0
4
0
[2, 3, 1, 0, 4, 5, 6, 7, 8, 9]
element number 5
This is 5
Previous is 4
element number 6
This is 6
Previous is 5
element number 7
This is 7
Previous is 6
element number 8
This is 8
Previous is 7
element number 9
This is 9
Previous is 8
element number 1
This is 2
Previous is -1
element number 0
This is 3
Previous is 2
element number 3
This is 1
Previous is 3
Switch triggered
a is 1
b is 2
3
1
3
1
[2, 1, 3, 0, 4, 5, 6, 7, 8, 9]
element number 2
This is 0
Previous is 3
Switch triggered
a is 2
b is 3
3
0
3
0
[2, 1, 0, 3, 4, 5, 6, 7, 8, 9]
element number 4
This is 4
Previous is 3
element number 5
This is 5
Previous is 4
element number 6
This is 6
Previous is 5
element number 7
This is 7
Previous is 6
element number 8
This is 8
Previous is 7
element number 9
This is 9
Previous is 8
element number 0
This is 2
Previous is -1
element number 1
This is 1
Previous is 2
Switch triggered
a is 0
b is 1
2
1
2
1
[1, 2, 0, 3, 4, 5, 6, 7, 8, 9]
element number 1
This is 0
Previous is 2
Switch triggered
a is 1
b is 2
2
0
2
0
[1, 0, 2, 3, 4, 5, 6, 7, 8, 9]
element number 3
This is 3
Previous is 2
element number 4
This is 4
Previous is 3
element number 5
This is 5
Previous is 4
element number 6
This is 6
Previous is 5
element number 7
This is 7
Previous is 6
element number 8
This is 8
Previous is 7
element number 9
This is 9
Previous is 8
element number 0
This is 1
Previous is -1
element number 1
This is 0
Previous is 1
Switch triggered
a is 0
b is 1
1
0
1
0
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
element number 2
This is 2
Previous is 1
element number 3
This is 3
Previous is 2
element number 4
This is 4
Previous is 3
element number 5
This is 5
Previous is 4
element number 6
This is 6
Previous is 5
element number 7
This is 7
Previous is 6
element number 8
This is 8
Previous is 7
element number 9
This is 9
Previous is 8
element number 0
This is 0
Previous is -1
element number 1
This is 1
Previous is 0
element number 2
This is 2
Previous is 1
element number 3
This is 3
Previous is 2
element number 4
This is 4
Previous is 3
element number 5
This is 5
Previous is 4
element number 6
This is 6
Previous is 5
element number 7
This is 7
Previous is 6
element number 8
This is 8
Previous is 7
element number 9
This is 9
Previous is 8
final list is [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Now quite frankly, this was a confusing process for myself. You may notice that my code is actually a for loop nested inside a for loop with an excess of print commands for debugging purposes. Here's what's going on. I'll explain the nested for loop first before explaining why it's necessary to nest it... since that will make sense later.

Before the nested for loop is created, the variable prev is set to negative one. I'll explain why that exists once I introduce the iteration.

For every element in the list, I print the element number, what the element is, and what the previous element is. This is helpful for debugging. For a reason I'm not aware of, the element number tends to get bugged. But it's only debugging so it should be OK as long as I know what's going on (and with all the other commands functioning as necessary, it's not that hard)

I then use an if statement with the condition that the present element i is less than the previous element prev. prev was set to negative one initially since the first element in the list cannot be swapped with a previous element, and negative one will always cause the condition to be False.

In the first iteration, the else statement will always be executed, which sets the variable prev to i for use in the next iteration.

If the if condition is fulfilled, a print statement is executed to let me know that this happened. I then set variable a to the index of the previous element and b to the index of the current element. I print them for good measure, as well as the values of the corresponding elements. The next command switches the elements in the actual list (see the source in the comment). I then print the element values in opposite order (the values appearing in the same order verifies that the elements were switched). I then print the list to verify the swap, and then set prev to the value of the second element of the pair, which will be the first element of the next pair.

Now that I've done a line-by-line explanation, I'll make a general summary of the functionality of the nested for loop. The nested for loop iterates through a list and swaps two elements if the first element is greater than the second. On one execution, the loop is very good at moving single elements to the other side of the list (as is the case with element of value 9).

However, elements of lesser value fall out of focus even if they are not in order. The original for loop was not nested and I was left with the problem of the element with value 8 being stuck at the beginning of my list (since I moved on from it in the first iteration of the loop). As such, I needed to find a way to execute this loop multiple times to move 8 up beside 9. I therefore nested this for loop inside another for loop that was to execute once for each element in the list. This guarantees the loop would execute enough times to move the greater-valued elements to the end of the list. I'll now give a condensed version of the code below

list = [9, 8, 4, 3, 5, 2, 6, 7, 1, 0]
print(list)

for i in list:
        prev = -1
        for i in list:
                if i < prev:
                        a = list.index(prev)
                        b = list.index(i)
                        # idea from https://stackoverflow.com/questions/2493920/how-to-switch-position-of-two-items-in-a-python-list
                        list[a], list[b] = list[b], list[a]
                        prev = list[b]
                else:
                        prev = i
print("final list is " +str(list))
[9, 8, 4, 3, 5, 2, 6, 7, 1, 0]
final list is [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]