Dear help

I am struggling to figure out why the following sorting algorithm doesn't work. Please help if you can. :)

>>

#myList is a list of arbitrary length

for i in range (len(myList)):
for j in range (i, len(myList) - 1):
if myList < myList[j]:
myList, myList[j] = myList[j], myList

Recommended Answers

All 2 Replies

I think j should be in range(i+1, len(myList)).

You should use Python's sort() function for lists. In any case, "doesn't work" is not descriptive enough. Include some test input, your output, and the desired output if you want to write the sort code yourself.

In addition to what Gribouillis said, you have it backwards. (Please don't use "i", "l", or "o" as single digit variables as they can look like numbers.)

for j in range (len(myList-1)):
    for k in range (j+1, len(myList)):
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.