Skip to content
Permalink
main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
'''Program to sort array in ascending order using Selection Sort.'''
def selectionSort(A):
'''A function for implementation of selection sort.'''
for i in range(len(A) - 1): # iterate through the array except the last value
currentMin = i # set the position of first element in unsorted array as minimum
for j in range(i + 1, len(A)): # go through values in array that were not compared
if A[j] < A[currentMin]: # find the smallest element in unsorted array
currentMin = j # set the position of the smallest element in unsorted array
swap(A, i, currentMin) # swap the unsorted value with the smallest element
return A # return sorted array
def swap(A, i, currentMin):
'''Swap two elements in array.'''
tempVar = A[i] # temporary variable that stores the value that will change
A[i] = A[currentMin] # set the smallest element of unsorted list at correct position
A[currentMin] = tempVar # set the replaced element at position of the smallest element
a = [6, 11, 8, 2, 15, 9]
print(selectionSort(a))