0

Here is the sorting code below. The first version had "for j in range(i+1, len(L)):" instead and that properly sorted it in increasing order. Thank you.

def modSwapSort(L): 
    """ L is a list on integers """
    print("Original L: ", L)
    for i in range(len(L)):
        for j in range(len(L)): # now it starts in the same place. will always be equal to start
            if L[j] < L[i]:
                L[j], L[i] = L[i], L[j] # swap the values
                print(L)
    print("Final L: ", L)
  • 2
    Does this answer your question: https://stackoverflow.com/questions/69437526/what-is-this-odd-sorting-algorithm? – Dani Mesejo Oct 17 '21 at 15:11
  • It seems this does a good job of explaining how the correct algorithm for sorting in increasing value works, but does not address why what I posted above sorts it in decreasing order instead – Beets Noms Oct 17 '21 at 15:31
  • 1
    It has the inverse conditional, so basically the same reasoning applies – Dani Mesejo Oct 17 '21 at 15:32
  • The conditional is the same actually. The only change is the range of the 2nd for loop. I get the first check will be checking the same value but then when it moves to to the next "j" I thought it would work the same as the other function with the range in "i+1". – Beets Noms Oct 17 '21 at 15:38
  • 1
    No it's not the same in your code is L[j] < L[i] and in the linked question is A[j] > A[i] – Dani Mesejo Oct 17 '21 at 15:42

0 Answers0