Write a function SelSortx that performs a selection sort on
Solution
from time import time
import random
#selection sort function. Returns sorted array
def sel_sort(vals):
#for loop to iterate through the list
for jj in range(0, len(vals)):
positionOfMin=0
for kk in range(jj+1, len(vals)):
if vals[kk]<vals[positionOfMin]: #testing if found a smaller value
positionOfMin = kk
#swapping values
temp = vals[jj]
vals[jj] = vals[positionOfMin]
vals[positionOfMin] = temp
#returning the sorted list
return vals
#all list lengths for which to test the function
vals_lengths = [10, 50, 100, 500, 1000, 5000, 10000, 50000]
#Will print a table of sorting time vs list length
print \"Sorting Time --- List Length\"
for vals_length in vals_lengths:
tstart = time()
sel_sort(random.sample(range(500000), vals_length))
tend = time()
ctime = tend-tstart
print ctime, \" \", vals_length
