Selection Sort

#!/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]

Comments (0)

  • To add your comment please or

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.

Got It!