0

How do I have the algorithm below to sort my list in opposite direction (right to left) instead of left to right.
Also, how do I make it sort simultaneously or alternatively in both directions? Thanks.

def bubblesort(l):
    for passes in range(len(l)-1, 0, -1):
        for index in range(passes):
            if l[index] < l[index + 1]:
                l[index], l[index + 1] = l[index + 1], l[index]
                print l
    return l

bubblesort([4,2,7,9,25])

Editor:
Please use code tags with your code to preserve the indentations. Otherwise the code is very difficult to read and not too many folks will help.

[code=python]
your Python code here

[/code]

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by vegaseat
0

In your code line:
if l[index] < l[index + 1]:
use:
if l[index] > l[index + 1]:
for ascending sort order.

BTW, programmers avoid using the letter l for a variable name, since it looks so much like 1.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.