Permalink
Cannot retrieve contributors at this time
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?
5003CEM/STD_1.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
22 lines (17 sloc)
1.23 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''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)) |