Two built-in functions, compare their difference:
list_var.sort()
sorted(list_var)
In-place sorting: does not require a secondary data structure
Out-of-place sorting: may require a secondary data structure
Selection sort uses in-place sorting
Watch visualization and describe: how selection sort works?
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
find_min_index
Now you found the index of smallest number, let’s make one swap: swap the items at index 0
and min_index
.
How to find the min_index
starting from begin_index
? You can call find_min_index
function.
How to find the min_index
starting from begin_index
? You can call find_min_index
function.
Now you found the index of smallest number starting from begin_index
, let’s make another swap:
selection_sort
that mutates the list argument and sort itfind_min_index
to find the index of smallest elementbegin_index
before loop, in each iteration add 1 to begin_index
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)
assert test_list == [5, 6, 7, 9, 10, 10, 12, 20, 43]
main()
[ 3, 1, 7, 2, 4 ] sweep, begin_index: 0
[ 1, 3, 7, 2, 4 ] swap and sweep, begin_index: 1
[ 1, 2, 7, 3, 4 ] swap and sweep, begin_index: 2
[ 1, 2, 3, 7, 4 ] swap and sweep: begin_index: 3
[ 1, 2, 3, 4, 7 ] swap
4 sweeps, 4 swaps (worst case scenario)
[ 3, 0, 7, 2, 4, 8 ] sweep
[ 0, 3, 7, 2, 4, 8] swap and sweep
[ 0, 2, 7, 3, 4, 8 ] swap and sweep
[ 0, 2, 3, 7, 4, 8 ] swap and sweep
[ 0, 2, 3, 4, 7, 8 ] swap and sweep
5 sweeps, 4 swaps
How many total sweeps and swaps to sort this list?
[ 7, 1, 1, 4, 2 ]
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
single_sweep_swap
def single_sweep_swap(numbers):
end = len(numbers) - 1
for i in range(end):
if numbers[i] > numbers[i+1]:
numbers[i], numbers[i+1] = numbers[i+1], numbers[i]
return numbers
def main():
assert single_sweep_swap([8, 3, 5, 7, 1]) == [3, 5, 7, 1, 8]
assert single_sweep_swap([3, 5, 7, 1, 9]) == [3, 5, 1, 7, 9]
main()
bubble_sort
that sweeps the list, comparing elementsend
starts at len(items)-1
end
indexReplace the two lines of comment with your code:
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, 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...
Flash Warning: the following video contains flashing lights. https://www.youtube.com/watch?v=kPRA0W1kECg
selection sort at 0:00, bubble sort at 4:00