Selection Sort

Python Programming

Python Programming Examples

#!/usr/bin/evn python

# Function for selection sort
def selectionSort(data):
    # Loop the process within range of number list
    for i in range(len(data)-1,0,-1):
        maxpos = 0
        for j in range(1,i+1):
            if data[j] > data[maxpos]:
                maxpos = j

        # Swaping data
        temp = data[i]
        data[i] = data[maxpos]
        data[maxpos] = temp

numList = [5,8,1,6,3,7,2,4,9]
print('Before sort:')
print(numList)
# Calling 'selectionSort' function by passing number array
selectionSort(numList)
print('After sort:')
print(numList)


# ***** Output *****
Before sort:
[5, 8, 1, 6, 3, 7, 2, 4, 9]
After sort:
[1, 2, 3, 4, 5, 6, 7, 8, 9]

Learn more about Selection Sort