0

my teacher recently wrote a program on how to sort a list with numbers and i dont understand mainly the boolean value statements and then the logic in the loop to sort the numbers so help would be appreciated in just explaining what he did. I have homework to do and sorting is part of it so im just trying to understand this example he did. Thanks

    d = [8, 14, 3, 5, 2, 23] # lists

    size = len( d )      # size = number of elements in list
    unsorted = True      # what does this really mean and do?

    while unsorted :     # bassicly while true, but while what is true? what would make it false?
        unsorted = False     #? did he just change the variable to false? if so, why, and
                         # how is the "while unsorted" before statement still being met
        i = 0                    # this bassiclly begins the indency number in the list below
        while i < size-1 :       # so while the indency number is less than the list 
                                 # element size it will loop through the rest
            if d[i] > d[i+1] :   # if the number in d[i]is greater than the number after
                temp = d[i]      # then variable temp gets assigned that number in d[i]
                d[i] = d[i+1]    # this confuses me. whats the purpose of setting d[i] to d[i+1]?
                d[i+1] = temp    # i think this has to do with the statement above, what does it
                unsorted = True  # why is this suddenly turned back to true?


            i += 1        # adds 1 to to indency or i until it reaches the list size to stop loop
    print d

Output ends up being a sorted list below

[2, 3, 5, 8, 14, 23]

Thanks

Zach Santiago
  • 381
  • 7
  • 18

2 Answers2

1

This is the Bubble sort sorting algorithm. To sort all elements of an array in ascending order this algorithm compares two neighbouring elements, and swaps their location if the successor i+1 of an element i has the smaller value.

Now lets comment some of your comments ;-)

unsorted = True      # what does this really mean and do?

This declares and initializes your boolean value. If False you wouldn't be able to enter the following while loop.

while unsorted :     # bassicly while true, but while what is true? what would make it false?
    unsorted = False     #? did he just change the variable to false? if so, why, and
                     # how is the "while unsorted" before statement still being met

The condition for execution of a while loop only gets checked before entering a new "round". Please check how a while loop works, this is a fundamental construct! The variable unsorted is set to False so the program is able to leave the loop, when the array has been sorted entirely.

i = 0  # this bassiclly begins the indency number in the list below

Yes, indeed Python uses zero based indexing (another term you should look up). This means that the first element in an array comes with the index zero

while i < size-1 :  # so while the indency number is less than the list 
                # element size it will loop through the rest

This makes you able to loop over all elements of the array. But be aware that this line may provoke an error. It really should be:

while i < size-2

size-1 is the index of the last element in an array of length size. But since you always compare an element and its successor you don't have to check the last element of the array (it doesn't have an successor).

   temp = d[i]      # then variable temp gets assigned that number in d[i]
   d[i] = d[i+1]    # this confuses me. whats the purpose of setting d[i] to d[i+1]?
   d[i+1] = temp    # i think this has to do with the statement above, what does it

This is the swapping I told you about. Elements d[i] and d[i+1] switch places. To do so you need an temporary storage for one variable.

unsorted = True  # why is this suddenly turned back to true?

Because he had to change the order of the elements in the array. The program should only be allowed to leave the outer while loop when no more swapping is necessary and the array elements have been sorted.

Community
  • 1
  • 1
el_tenedor
  • 644
  • 1
  • 8
  • 19
1

This is a bubble sort. The concept of a bubble sort is basically swapping larger numbers towards the end of the list. The Boolean variable is used for keeping track of whether or not the list is sorted.
We know that the list is sorted if we checked every number and we don't have to swap any of them. (That's basically what the code does and the reason we need the Boolean variable)


unsorted = True # what does this really mean and do?
This keeps tracks of whether or not the list is sorted. When this is False, we are done sorting and we can print the list. However, if it is True, we have to check the list and swap the numbers to the correct spot.


while unsorted : # bassicly while true, but while what is true? what would make it false?
As I mentioned, while True: means that the list is not sorted last time we checked, so we have to check the list again(i.e. run the code in the while loop.)


unsorted = False
This might be the tricky part. We just assume that the list the sorted, unless we have to swap numbers. (The code below is the piece of code that do the swapping)

if d[i] > d[i+1] :   
    temp = d[i]    # store the larger number in a temporary variable   
    d[i] = d[i+1]  # put the smaller number in the spot of the larger number
    d[i+1] = temp  # put the larger number after the smaller number
    unsorted = True # we swapped a number, so this list might not be completely sorted


J.Ku
  • 123
  • 7