Search your topic here

Python Program to perform selection sort

To write a Python Program to perform selection sort.

def selection_sort(alist):
    for i in range(0, len(alist) - 1):
        smallest = i
        for j in range(i + 1, len(alist)):
            if alist[j] < alist[smallest]:
                smallest = j
        alist[i], alist[smallest] = alist[smallest], alist[i]


alist = input('Enter the list of numbers: ').split()
alist = [int(x) for x in alist]
selection_sort(alist)
print('Sorted list: ', end='')
print(alist)


OUTPUT:

Enter the list of numbers: 1 4 2 3 6 8 9 7 5
Sorted list: [1, 2, 3, 4, 5, 6, 7, 8, 9]

LEAVE A REPLY