Home > Programming > Selection sort in Python

Selection sort in Python


Python has lists that come very handy in doing many complex works such as finding min, max, sort etc., Here I’ll show sorting a list using selection sort and the other function of one line sort() function.

#Author: Vasanth Raja Chittampally

numbers=[]
n=int(raw_input("Enter n:"))
for i in range(0,n):
    numbers.insert(i,int(raw_input("Enter numbers[%d]: " %i)))

for i in range(0,n):
    min =numbers[i]
    min_indx=i
    for j in range(i+1,n) :
        if min > numbers[j] :
            min =numbers
            min_indx=j
    temp=numbers[min_indx]
    numbers[min_indx]=numbers[i]
    numbers[i]=temp
print numbers

The above same thing can be done with just one line built in function of python sort(). like “numbers.sort()”

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: