Jed Rembold
November 24, 2025
3198345-61d515c2You want to look for the term "fish" in
the list to the right. What search method would prove fastest in this
specific case?
list_to_search = [
"onions",
"puppies",
"fish",
"donkey",
"goats",
"carrots",
"lasagna",
"sheep",
"bears",
"beets",
"battlestar galactica"
]
The easiest sorting algorithm to explain is that of selection sort
Imagine your left hand keeping track of where you were in the array, and your right hand scanning through and finding the next smallest value to move to that location each iteration
def selection_sort(array):
for lh in range(len(array)):
rh = lh
for i in range(lh+1, len(array)):
if array[i] < array[rh]:
rh = i
array[lh], array[rh] = array[rh], array[lh]def selection_sort(array):
for lh in range(len(array)):
rh = lh
for i in range(lh+1, len(array)):
if array[i] < array[rh]:
rh = i
array[lh], array[rh] = array[rh], array[lh]
L = [31, 41, 59, 26, 53, 58, 97, 93, 23, 84]
selection_sort(L)
| Array Size | Running Time (sec) | Increase of |
|---|---|---|
| 10 | 0.000013 s | |
| 100 | 0.000581 s | 44x |
| 1,000 | 0.0578 s | 99x |
| 10,000 | 5.738 s | 99x |
| 100,000 | 574.2 s | 100x |
| 1,000,000 | 57395 s | 100x |
for
loop comparison
| \(N\) | \(\tfrac{N\times(N+1)}{2}\) | Increase of |
|---|---|---|
| 10 | 55 | |
| 100 | 5,050 | 92x |
| 1,000 | 500,500 | 99x |
| 10,000 | 50,005,000 | 100x |
| 100,000 | 5,000,050,000 | 100x |
What would be the big-O complexity of the below function?
def func(array):
n = 0
while n < len(array):
for i in range(n):
array[i] = array[n]
n += 2