In this article, you will learn how to write simple Selection Sort Algorithm in Python.
Code:
def selectionsort(alist):
for i in range(len(alist)):
smallest_no = min(alist[i:])
index = alist.index(smallest_no)
alist[i], alist[index] = smallest_no, alist[i]
i = i + 1
return alist
if __name__ == '__main__':
num = int(raw_input("Enter total numbers which you are going to entered "))
nlist = []
while num > 0:
no = raw_input("Enter the number")
nlist.append(no)
num = num - 1
result = selectionsort(nlist)
print result
Output:
Enter total numbers which you are going to entered 5
Enter the number8
Enter the number3
Enter the number4
Enter the number1
Enter the number2
['1', '2', '3', '4', '8']