Process Overview
Sorting algorithms arrange items in order. Bubble sort swaps neighbors to push the largest to the end. Selection sort finds the smallest and places it at the start. Both repeat until sorted.
Jump into concepts and practice - no test required
Sorting algorithms arrange items in order. Bubble sort swaps neighbors to push the largest to the end. Selection sort finds the smallest and places it at the start. Both repeat until sorted.
Array Memory Layout:
Index: 0 1 2 3
+----+----+----+----+
Value: | 4 | 2 | 5 | 1 |
+----+----+----+----+
During sorting, values swap places in these boxes.
Bubble Sort pushes largest values to the right step by step.
Selection Sort picks smallest values and places them at the left step by step.arr in Python?for i in range(len(arr)): correctly loops over indices; others are incorrect or off-by-one.[4, 2, 5, 1]?Initial list: [4, 2, 5, 1] Pass 1: Compare and swap neighbors if needed
arr = [3, 1, 4]
for i in range(len(arr)):
min_idx = i
for j in range(i+1, len(arr)):
if arr[j] < arr[min_idx]:
min_idx = j
arr[i], arr[min_idx] = arr[min_idx], arr[i]
print(arr)[7, 3, 5, 2, 9]. After two full passes of selection sort, what will the list look like?