# L-13 MCS 275 Wed 13 Feb 2008 : selectsort.py

# In selection sort we select the minimum of
# the list and put it in first place.
# The minimum of the rest of the list is
# put in second place, etc...

from random import randint

def PSelectSort(L):
   """
   Sorts the data in L
   in increasing order.
   This is the inPlace version.
   """
   for k in range(0,len(L)-1):
      S = L[k:len(L)]
      m = min(S)
      i = k + S.index(m)
      if i != k:
         L[i] = L[k]
         L[k] = m

def ISelectSort(L):
   """
   Returns a list with the data of L
   sorted in increasing order.
   This is the Iterative version.
   """
   S = []
   while len(L) > 0:
      m = min(L)
      S.append(m)
      L.pop(L.index(m))
   return S

def RSelectSort(L):
   """
   Returns a list with the data of L
   sorted in increasing order.
   This is the Recursive version.
   """
   if len(L) <= 1:
      return L
   else:
      m = min(L)
      L.pop(L.index(m))
      return [m] + RSelectSort(L)

def main():
   """
   Calls MergeSort on a list of
   randomly generated numbers.
   """
   a = input('Give lower bound : ')
   b = input('Give lower bound : ')
   N = input('How many numbers ? ')
   r = lambda i: randint(a,b)
   L = map(r,range(0,N))
   import copy
   K = copy.deepcopy(L)
   M = copy.deepcopy(L)
   print 'L =', L
   S = ISelectSort(L)
   print 'S =', S
   S = RSelectSort(K)
   print 'S =', S
   PSelectSort(M)
   print 'S =', M

main()
