Selection Sort
def findLargest(listOfValues, start):
largestSoFar = listOfValues[start]
largestIndex = start
current = start+1
while (current < len(listOfValues)):
if (listOfValues[current] > largestSoFar):
largestSoFar = listOfValues[current]
largestIndex = current
current = current + 1
return (largestIndex)
def selectionSort(listOfValues):
leftEdge = 0
while (leftEdge < (len(listOfValues)-1)):
biggestPosition = findLargest(listOfValues, leftEdge)
temp = listOfValues[biggestPosition]
listOfValues[biggestPosition] = listOfValues[leftEdge]
listOfValues[leftEdge] = temp
leftEdge = leftEdge + 1
print listOfValues
- If we count number of comparisons, what is the best case performance for this algorithm on a list of size n?
- If we count number of comparisons, what is the worst case performance for this algorithm on a list of size n?