Tuesday, January 22, 2008

Selection sort in python

I'm learning python, so I decided to write selection sort in it. I converted the CPP code from 24btes.com. Here's the code (the array at the beginning contains the numbers that will be sorted):


a = [90, 96, 74, 10, 44, 53, 61, 77, 37, 52, 25, 60, 9, 14, 20, 93, 5, 59, 29, 86, 6, 64, 4, 38, 51, 24, 11, 16, 85, 69, 34, 62, 50, 87, 55, 92, 98, 47, 46, 91, 57, 36, 23, 97, 3, 48, 83, 99, 12, 31, 2, 76, 28, 27, 39, 68, 49, 58, 95, 21, 17, 79, 71, 45, 65, 70, 33, 56, 43, 35, 81, 75, 72, 26, 19, 13, 30, 22, 1, 15, 63, 18, 8, 73, 66, 67, 54, 100, 40, 89, 78, 7, 42, 32, 84, 88, 94, 80, 82, 41]

def selSort (array):
i = 0
listLen = len(array)
while i < (listLen - 1):
minat = i
minval= array[i]

j = i + 1
while j < listLen:
if minval> array[j]:
minat = j
minval= array[j]
j = j + 1

temp = array[i]
array[i] = array[minat]
array[minat] = temp
i = i + 1

print array
print listLen


selSort (a)

3 comments:

Raisa said...

Good words.

Vijender Meena said...

great best out of all i find on google

Vijender Meena said...

great

best algorithm i found on google