user1950302
user1950302

Reputation: 29

selection sort in python

Here's a code for selection sort but it doesn't print the sorted list. How can I show it?

badlist = input("Enter list: ")  
def select(badlist):  
        l = list[:]  
        sorted = []  
        while len(l):  
            lowest == l[0]  
            for x in l:  
                if x < lowest:  
                    lowest = x  
            sorted.append(lowest)  
            l.remove(lowest)  
        return sorted  
select(badlist)  

Upvotes: 0

Views: 1682

Answers (3)

Mark Byers
Mark Byers

Reputation: 837946

Use print.

If you are using Python 2.x print is a keyword:

result = select(badlist)
print result

In Python 3.x print is a function and you must use parentheses:

result = select(badlist)
print(result)

You also have at least two other errors:

  • Your function parameter is called badlist but you never use it.
  • The == operator is equality comparison. You need = for assignment.

Also your algorithm will be very slow, requiring O(n2) operations.

Upvotes: 0

Developer
Developer

Reputation: 8400

if you type select(badlist) in Python shell is should show the result however if you're running your script as file you need to use print statement, as print select(badlist).

Upvotes: 2

Inbar Rose
Inbar Rose

Reputation: 43437

Expanding on my comment:

Why not simply use the builtin sorted(list) and then you can just have your whole code be:

print sorted(input("Enter list: "))

Upvotes: 1

Related Questions