Algorithm:
begin
for i = 0 to size-2
do
min = i
for j = i+1 to size – 1
do
if array[j] < array[Min] then
Min = j
done
swap array[i] with array[Min].
done
end
Code:
def selection_sort(alist):
for i in range(0,len(alist)-1):
min=i
for j in range(i+1,len(alist)):
if(alist[min]>alist[j]):
min=j
alist[i],alist[min]=alist[min],alist[i]
if __name__ == "__main__":
alist=input('Enter the list of numbers: ').split()
alist=[int(x) for x in alist]
selection_sort(alist)
print(f'Your sorted list is: {alist}')
Complexity:
Worst Complexity: n^2
Average Complexity: n^2
Best Complexity: n^2
Space Complexity: 1
Comments
Post a Comment
If you've any doubts, please let me text