Home > Software engineering >  Can someone help me understand how this piece of code to sort elements of a list in descending order
Can someone help me understand how this piece of code to sort elements of a list in descending order

Time:11-24

I just would like to understand the passages of how this piece of code operates:

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

for i in range(len(llist)):
    for j in range(i 1):
        if llist[j] < llist[i]:
            temp = llist[i]
            llist[i] = llist[j]
            llist[j] = temp

print(llist)

I tried to meticulously go through every passage of the nested loop, but I can't understand how at the end of all that new-variable-assigning the elements of the list get sorted from highest to lowest.

Thank you very much to anyone who can help

CodePudding user response:

With a bit of printing:

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

for i in range(len(llist)):
  print()
  print(f'current pivot is {llist[i]} on position {i}')

  for j in range(i 1):
    
    if llist[j] < llist[i]:
      print('-----------------------------------------------------------------')
      print((f'\t {llist[j]} in position {j} and {llist[i]} on position {i} are in'
            ' the wrong order'))
      print(f'\t before swapping our list is : {llist}')
      temp = llist[i]
      llist[i] = llist[j]
      llist[j] = temp
      print(f'\t after swapping we have {llist}')
      print('-----------------------------------------------------------------')
  print(f'list at the end of iteration: {llist}')

You get:

current pivot is 5 on position 0
list at the end of iteration: [5, 2, 1, 4, 3]

current pivot is 2 on position 1
list at the end of iteration: [5, 2, 1, 4, 3]

current pivot is 1 on position 2
list at the end of iteration: [5, 2, 1, 4, 3]

current pivot is 4 on position 3
-----------------------------------------------------------------
     2 in position 1 and 4 on position 3 are in the wrong order
     before swapping our list is : [5, 2, 1, 4, 3]
     after swapping we have [5, 4, 1, 2, 3]
-----------------------------------------------------------------
-----------------------------------------------------------------
     1 in position 2 and 2 on position 3 are in the wrong order
     before swapping our list is : [5, 4, 1, 2, 3]
     after swapping we have [5, 4, 2, 1, 3]
-----------------------------------------------------------------
list at the end of iteration: [5, 4, 2, 1, 3]

current pivot is 3 on position 4
-----------------------------------------------------------------
     2 in position 2 and 3 on position 4 are in the wrong order
     before swapping our list is : [5, 4, 2, 1, 3]
     after swapping we have [5, 4, 3, 1, 2]
-----------------------------------------------------------------
-----------------------------------------------------------------
     1 in position 3 and 2 on position 4 are in the wrong order
     before swapping our list is : [5, 4, 3, 1, 2]
     after swapping we have [5, 4, 3, 2, 1]
-----------------------------------------------------------------
list at the end of iteration: [5, 4, 3, 2, 1]

So we you can see, every time it finds that a number on the list has another number to its left that is smaller than the number at hand, then it swaps them.

For 5, it does nothing as there are no numbers to its left.

For 2, it does nothing as 5 is the only number to the left and it's > 2.

For 1, it does nothing as 5 and 2 are the only numbers to the left and they are both > 1.

For 4, it finds 2 as a number to its left, so we swap them and have:

[5,2,1,4,3] -> [5,4,1,2,3]

So now we continue and find 1 to the left of 2 (since now 2 is in the 3rd position), so we swap again

[5,4,1,2,3] -> [5,4,2,1,3]

Does that help?

  • Related