lec09+Searching+&+Sorting
lec09+Searching+&+Sorting
Searching
• You have a list.
• How do you find something in the list?
5 2 3 4
Found 3.
Linear Search
Idea: go through the list from start to finish
Implemented as a function:
def linear_search(value, lst):
for i in lst:
if i == value:
return True
return False
Why?
IDEA
If list is sorted, we can
“divide-and-conquer”
≤𝑥 ≥𝑥
𝑎 𝑥
0 1 … 𝑘 … 𝑛−2 𝑛−1
≤𝑥 ≥𝑥
𝑂(log 𝑛)
Wishful Thinking
We assumed the list was sorted.
Unsorted
Sorted Smallest
Obvious Way
Find the smallest card not in hand (SCNIH), and put it at the
end of your hand. Repeat.
Unsorted
Sorted Smallest
Obvious Way
Find the smallest card not in hand (SCNIH), and put it at the
end of your hand. Repeat.
Unsorted
Sorted Smallest
Obvious Way
Find the smallest card not in hand (SCNIH), and put it at the
end of your hand. Repeat.
Unsorted
Sorted Smallest
Obvious Way
Find the smallest card not in hand (SCNIH), and put it at the
end of your hand. Repeat.
Unsorted
Sorted Smallest
Obvious Way
Find the smallest card not in hand (SCNIH), and put it at the
end of your hand. Repeat.
Unsorted
Sorted Smallest
Obvious Way
Find the smallest card not in hand (SCNIH), and put it at the
end of your hand. Repeat.
Unsorted
Sorted Smallest
Obvious Way
Find the smallest card not in hand (SCNIH), and put it at the
end of your hand. Repeat.
Unsorted
Sorted Smallest
Obvious Way
Find the smallest card not in hand (SCNIH), and put it at the
end of your hand. Repeat.
Unsorted
Sorted Smallest
Obvious Way
Find the smallest card not in hand (SCNIH), and put it at the
end of your hand. Repeat.
Repeat
Unsorted
Sorted Smallest
Obvious Way
Find the smallest card not in hand (SCNIH), and put it at the
end of your hand. Repeat.
Repeat
Unsorted
Sorted Smallest
Obvious Way
Find the smallest card not in hand (SCNIH), and put it at the
end of your hand. Repeat.
Repeat
Unsorted
Sorted Smallest
Obvious Way
Find the smallest card not in hand (SCNIH), and put it at the
end of your hand. Repeat.
Repeat
Unsorted
Sorted Smallest
Obvious Way
Find the smallest card not in hand (SCNIH), and put it at the
end of your hand. Repeat.
Repeat
Unsorted
Sorted Smallest
Obvious Way
Find the smallest card not in hand (SCNIH), and put it at the
end of your hand. Repeat.
Repeat
Unsorted
Sorted Smallest
Obvious Way
Find the smallest card not in hand (SCNIH), and put it at the
end of your hand. Repeat.
Repeat
Unsorted
Sorted Smallest
Obvious Way
Find the smallest card not in hand (SCNIH), and put it at the
end of your hand. Repeat.
Repeat
Unsorted
Sorted Smallest
Obvious Way
Find the smallest card not in hand (SCNIH), and put it at the
end of your hand. Repeat.
Repeat
Unsorted
Sorted Smallest
Obvious Way
Find the smallest card not in hand (SCNIH), and put it at the
end of your hand. Repeat.
Repeat
Unsorted
Sorted Smallest
Obvious Way
Find the smallest card not in hand (SCNIH), and put it at the
end of your hand. Repeat.
Repeat
Unsorted
Sorted Smallest
Obvious Way
Find the smallest card not in hand (SCNIH), and put it at the
end of your hand. Repeat.
Done
Unsorted
Sorted Smallest
There is actually a name for this:
Selection Sort!
Let’s Implement it!
a = [4,12,3,1,11]
sort = []
while a: # a is not []
smallest = a[0]
for element in a:
if element < smallest:
smallest = element
a.remove(smallest)
sort.append(smallest)
print(a)
Output
[4, 12, 3, 11]
[4, 12, 11]
[12, 11]
[12]
[]
print(a)
[]
print(sort)
[1, 3, 4, 11, 12]
Order of Growth?
• Time: Worst 2
𝑂(𝑛 )
Average 𝑂(𝑛2)
Best 𝑂(𝑛2)
How to merge?
How to merge?
• Compare first element
• Take the smaller of the two
• Repeat until no more elements
Merging
def merge(left, right):
results = []
while left and right:
if left[0] < right[0]:
results.append(left.pop(0))
else:
results.append(right.pop(0))
results.extend(left)
results.extend(right)
return results
Order of Growth?
• Time: Worst 𝑂(𝑛 log 𝑛)
Average 𝑂(𝑛 log 𝑛)
Best 𝑂(𝑛 log 𝑛)
• Space: 𝑂(𝑛)
No need to memorize
Sort Properties
In-place: uses a small, constant amount of
extra storage space, i.e., 𝑂(1) space
Create 100M of
numbers, estimated
to be 400MB of data
Output:
Let’s calculate
• 400M of data needs 6 seconds
• 15 Exabyte of data needs how long?
- 15 EB = 15 x 1024 x 1024 x 1024 x 1024 MB
- To search through 15EB of data…..
•7845 years…..