Professional Documents
Culture Documents
CS1010S Lecture 07 - Searching & Sorting
CS1010S Lecture 07 - Searching & Sorting
Lecture 7
Searching & Sorting
5 Oct 2022
Saying of the Wise
I hear and I forget. 学 知 见 闻 不
I see and I remember. 至 之 之 之 闻
于 不 不 不 不
I do and I understand. 行 若 若 若 若
- Xun Zi 之 行 知 见 闻
而 之 之 之 之
止
矣
Some Philosophy
Python is like chess Lecture
- Easy to learn, hard to master Trainings
50
40
Level
30
20
Maximum
10 Typical
Minimum
0
1 2 3 4 5 6 R 7 8 9 10 11 12 13
Week
Difficulty Curve
Midterm Test
1 2 3 4 5 6 R 7 8 9 10 11 12 13
Week
Plagiarism
The act of presenting another’s work or idea
that as your own.
Academic Honesty Policy
• Not allowed
- receive any part of a solution to assignments prior
to submitting your own
- provide any part of a solution to a classmate when
it is they, and not you, who has not solved it
Academic Honesty Policy
• Allowed
- showing erroneous code you have written to a friend who
have submitted their assignment for help to identify the
error
- help a classmate identify, but not fix, the error in their
code, after you have submitted your assignment
- group discussion of assignments without producing a
solution for reference
David J. Malan, Brian Yu, and Doug Lloyd. 2020. Teaching Academic Honesty in CS50. In The 51st ACM Technical Symposium on Computer Science
Education (SIGCSE ’20), March 11–14, 2020, Portland, OR, USA. ACM, New York, NY, USA, 7 pages.
Regressive Scoring
• Current scoring uses a weighted average
- Midterm 15%
- Final 40%
• Poor midterm can pull down score
- Worst case: 0 for midterm, 100 for final
0Τ 100Τ 40
- 15% × 60 + 40% × 100 = Τ55
Can we do better?
• Redo midterms (cap at 60%)?
- Class is too large. Not tenable
• Adjust weightage?
- Too low, becomes meaningless
• Take best of both?
- Incorrect assessment. Final > Midterm
“Progressive” Scoring
if final > midterm:
return final # ignore midterm
else:
return weighted_avg(final, midterm) # no worse
t = tuple(range(5)) l = list(range(5))
→ (0, 1, 2, 3, 4) → [0, 1, 2, 3, 4]
t[4] → 4 l[4] → 4
t[2:] → (2, 3, 4) l[2:] → [2, 3, 4]
Sequence Operations
Tuple List
a = (1, 2, 3, 4) a = [1, 2, 3, 4]
b = (5, 6, 7, 8) b = [5, 6, 7, 8]
c = a + b c = a + b
c → (1, 2, 3, 4, 5, 6, 7, 8) c → [1, 2, 3, 4, 5, 6, 7, 8]
Appending element to list
With Tuples, we can only create new tuples
tpl = (1, 2, 3) tpl
tpl = tpl + (4,) 1 2 3 4
lst += [4, 5, 6]
lst →[1, 2, 3, 4, 5, 6]
lst2 →[1, 2, 3, 4, 5, 6]
lst == lst2 →True
lst is lst2 →True
Mutable versus Immutable
tup = (1, 2, 3)
tup2 = tup
tup == tup2 → True
tup is tup2 → True
tup += (4, 5, 6)
tup (1, 2, 3, 4, 5, 6)
tup2 (1, 2, 3)
tup == tup2 → False
tup is tup2 → False
Deleting Elements
>>> a = [-1, 1, 66.25, 333, 333, 1234.5]
>>> del a[0]
>>> a
[1, 66.25, 333, 333, 1234.5]
lst.copy()
returns a shallow copy of a list
lst.insert(<pos>, <element>)
inserts element into position pos
List Operations
lst.pop(<pos>)
returns and remove element at position pos. If pos is omitted,
removes the last element
lst.remove(<element>)
removes first occurrence of element from list, error if element is not
in the list.
lst.clear()
clears the list
List Operations
s = [1, 2, 3, 4, 5]
t = s.copy() # t = [1, 2, 3, 4, 5]
s.reverse() # s = [5, 4, 3, 2, 1]
# t = [1, 2, 3, 4, 5]
s.insert(0, 1) # s = [1, 5, 4, 3, 2, 1]
s.pop() # 1
# s = [1, 5, 4, 3, 2]
s.pop(1) # 5
# s = [1, 4, 3, 2]
s.insert(2, 2) # s = [1, 4, 2, 3, 2]
s.remove(4) # s = [1, 2, 3, 2]
s.clear() # s = []
Iterating on Lists
We can iterate through lists using for operator
>>> s = [1, 2, 3, 4, 5]
>>> for element in s:
print(element)
1
2
3
4
5
List Comprehension
We can iterate also generate new lists
>>> s = [1, 2, 3, 4, 5]
>>> t = [n**2 for n in s]
[1, 4, 9, 16, 25]
5 2 3 4
Found 3.
Linear Search
Idea: go through the list from start to finish
# equivalent code
for i in [5, 2, 3, 4]:
if i == 3:
return True
Linear Search
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
>>> a.sort()
>>> print(a)
[1, 2, 3, 4, 7, 31, 32, 34]