Sorting Algorithms
Example: Selection Sort
def selectionSort(array):
for i in range(len(array)):
smallest = None
location = 0
for j in range(i+1, len(array)):
if smallest is None:
smallest = array[j]
location = j
elif array[j] < smallest:
smallest = array[j]
location = j
# end of inner for
if smallest < array[i]:
# Python Swap
array[i], array[location] = array[location], array[i]
# end of outer loop
return array
# end of selectionSort()
def selectionSort2(array):
# A two list approach
sorted_list = []
while array:
smallest = min(array)
array.remove(smallest)
sorted_list.append(smallest)
return sorted_list
# end of selectionSort2()What sorting algorithm does the Python use?
Using sorted()
sorted()Using .sort()
.sort()Connected Readings
PreviousDetermine if the difference between an integer from the array and the target value existsNextUsing Two Pointers
Last updated