My first implementation of a selection sort!

by Gilbert Keith

This is done in Python.

'''
Created on Jul 13, 2013

@author: gk
'''
import sys
print("\n")
arr=[]
sumOfArr=0
limit=int(input('Enter the number of values in your list:'))
for m in range(limit):
 #print("M: ",m)
 arr.append(int(input("Please enter value # %d:" % (m))))

print("\nwe are now done accepting values")
print("\nLet me print out the values you entered")

for index,item in enumerate(arr):
 print(index,item)

print("\n now let me sort the values you entered using selection sort.")

for counter in range(len(arr)):
 idxSmallestVal=counter
 smallestVal=sys.maxsize
 for j in range(counter,len(arr)):
 # print('Counter: %d, J: %d' % (counter,j))
 if arr[j]<smallestVal:
 smallestVal=arr[j]
 idxSmallestVal=j
 arr[idxSmallestVal]=arr[counter]
 arr[counter]=smallestVal
print('\n Done.')
print('\n Now let me attempt to print out your sorted values:')
for index,item in enumerate(arr):
 print(index,item)

This is what the output looks like:

Enter the number of values in your list:9
 Please enter value # 0:-465
 Please enter value # 1:-987
 Please enter value # 2:-8
 Please enter value # 3:-1234
 Please enter value # 4:8
 Please enter value # 5:0
 Please enter value # 6:0
 Please enter value # 7:-12678
 Please enter value # 8:-76543
we are now done accepting values
Let me print out the values you entered
 0 -465
 1 -987
 2 -8
 3 -1234
 4 8
 5 0
 6 0
 7 -12678
 8 -76543
now let me sort the values you entered using selection sort.
Done.
Now let me attempt to print out your sorted values: 
0 -76543
1 -12678 
2 -1234 
3 -987 
4 -465 
5 -8 
6 0 
7 0 
8 8 
done.

[/preformatted]

Advertisements