[5, 10, 20, 6, 7, 9, 43, 10, 12]
[5, 6, 7, 9, 10, 10, 12, 20, 43]
Come up with a sorting algorithm
Discuss ideas for how to sort numbers
Depict your algorithm with drawings/diagrams or with pseudocode
Can’t use the .sort() method
In-place sorting: does not require a secondary data structure
Out-of-place sorting: may require a secondary data structure
You have 10 minutes to complete the quiz
main()
, no test casesBuilt-in functions you can use: round()
, input()
, float()
, str()
, int()
, len()
, range()
, set()
, list()
— you don’t have to use all of these
Selection sort is a very simple sorting algorithm
Scan the list and find the smallest element
Swap this element with the beginning element
Continue these steps for the remaining list, not including the element just swapped
Repeat
Visualizing sorting algorithms with graphics can give one a better understanding
selection_sort
def find_min_index(items):
min_index = None
for i in range(len(items)):
if min_index == None or items[min_index] > items[i]:
min_index = i
return min_index
def selection_sort(items):
begin_index = 0
while begin_index < len(items)-1:
# get min of sublist, add begin_index to shift it
min_index = find_min_index(items[begin_index:]) + begin_index
items[begin_index], items[min_index] = items[min_index], items[begin_index]
begin_index += 1
def main():
test_list = [5, 10, 20, 6, 7, 9, 43, 10, 12]
selection_sort(test_list)
print(test_list)
main()
[5, 6, 7, 9, 10, 10, 12, 20, 43]
[3, 1, 7, 2, 4] sweep
[1, 3, 7, 2, 4] swap and sweep
[1, 2, 7, 3, 4] swap and sweep
[1, 2, 3, 7, 4] swap and sweep
[1, 2, 3, 4, 7] swap
4 sweeps
4 swaps (worst case scenario)
[ 3, 1, 7, 2, 4, 8, 5 ] sweep
[ 1, 3, 7, 2, 4, 8, 5 ] swap and sweep
[ 1, 2, 7, 3, 4, 8, 5 ] swap and sweep
[ 1, 2, 3, 7, 4, 8, 5 ] swap and sweep
[ 1, 2, 3, 4, 7, 8, 5 ] swap and sweep
[ 1, 2, 3, 4, 5, 8, 7 ] swap and sweep
[ 1, 2, 3, 4, 5, 7, 8 ] swap
6 sweeps, 6 swaps
Bubble Sort: another sorting algorithm
Scan through each element in the list, comparing the current element with the next one
If the next one is smaller, swap the elements
Continue these iterations until the whole list is sorted
This causes the large elements to “bubble up” to the top
bubble_sort
def bubble_sort(items):
end = len(items)-1
for i in range(len(items)-1):
for j in range(end):
if items[j] > items[j+1]:
items[j], items[j+1] = items[j+1], items[j]
end -=1
def main():
test_list = [5, 10, 20, 6, 7, 9, 43, 10, 12]
bubble_sort(test_list)
print(test_list)
main()
[5, 6, 7, 9, 10, 10, 12, 20, 43]
8 sweeps, 10 swaps
[5, 10, 20, 6, 7, 9, 43, 10, 12]
[5, 10, 6, 20, 7, 9, 43, 10, 12]
[5, 10, 6, 7, 20, 9, 43, 10, 12]
[5, 10, 6, 7, 9, 20, 43, 10, 12]
[5, 10, 6, 7, 9, 20, 10, 43, 12]
[5, 10, 6, 7, 9, 20, 10, 12, 43]
[5, 10, 6, 7, 9, 20, 10, 12, 43]
[5, 6, 10, 7, 9, 20, 10, 12, 43]
[5, 6, 7, 10, 9, 20, 10, 12, 43]
[5, 6, 7, 9, 10, 20, 10, 12, 43]
[5, 6, 7, 9, 10, 10, 20, 12, 43]
[5, 6, 7, 9, 10, 10, 12, 20, 43]
def bubble_sort(items):
is_sorted = False
end = len(items)-1
while not is_sorted:
is_sorted = True
for i in range(end):
if items[i] > items[i+1]:
items[i], items[i+1] = items[i+1], items[i]
is_sorted = False
end -= 1
def main():
test_list = [5, 10, 20, 6, 7, 9, 43, 10, 12]
bubble_sort(test_list)
print(test_list)
main()
[5, 6, 7, 9, 10, 10, 12, 20, 43]
3 sweeps, 10 swaps
[ 3, 1, 7, 2, 4 ] first sweep
[ 1, 3, 7, 2, 4 ] swap
[ 1, 3, 2, 7, 4 ] swap
[ 1, 3, 2, 4, 7 ] swap
[ 1, 3, 2, 4, 7 ] second sweep
[ 1, 2, 3, 4, 7 ] swap
[ 1, 2, 3, 4, 7 ] third sweep
[ 3, 1, 7, 2, 4, 8, 5 ] first sweep
[ 1, 3, 7, 2, 4, 8, 5 ] swap
[ 1, 3, 2, 7, 4, 8, 5 ] swap
[ 1, 3, 2, 4, 7, 8, 5 ] swap
[ 1, 3, 2, 4, 7, 5, 8 ] swap
[ 1, 3, 2, 4, 7, 5, 8 ] second sweep
[ 1, 2, 3, 4, 7, 5, 8 ] swap
[ 1, 2, 3, 4, 5, 7, 8 ] swap
[ 1, 2, 3, 4, 5, 7, 8 ] third sweep
Go to gradescope and answer today’s attendance question
There are many sorting algorithms
Bogo sort
Selection sort
Bubble sort
Insertion sort
Merge sort
Quick sort
...more...