Professional Documents
Culture Documents
Class 09
• Searching
• Sequential search
• Binary search
• Hashing
• Sorting
4
Searching
• The problem of searching is one of the most common problems that arise in
computing.
• Searching is the algorithmic process of finding a particular item in a collection
of items, returning True/False, and possibly where it is found.
• Membership in Python: the in operator.
>>> 15 in [3, 5, 2, 1]
False
>>> 3 in {3:"a", 5:[], 2:"4", 1:33}
True
• Easy to write, but what is the underlying process that is carried out?
• We are interested in how different algorithms for searching for an item work
and how they compare to one another.
pos = 0
while pos < len(a_list):
if a_list[pos] == item:
return True
else:
pos = pos + 1
return False
DESIGN OF ALGORITHMS USING PYTHON 2023/24
Analysis of Sequential Search
• To analyze searching algorithms, we need to decide on a basic unit of computation
- the common step that must be repeated in order to solve the problem.
• For searching: number of comparisons performed.
• Assuming that the probability that the item we are looking for is in any particular
position is exactly the same for each position of the list.
Class 09
• Searching
• Sequential search
• Binary search
• Hashing
• Sorting
12
Hashing
• A hash table is a collection of items which are stored in slots, in such
a way as to make it easy to find them later. Initially they are all empty.
require
knowing in
which slot to
find an item
Class 09
• Searching
• Sorting
• Bubble Sort
• Selection Sort
• Insertion Sort
• Merge Sort
• Quick Sort
23
Sorting
• Sorting is the process of placing elements from a collection in some kind of
order.
• Sorting is an important area of study in computer science, and there are many
sorting algorithms that have been developed and analyzed.
• Sorting a large number of items can take a substantial amount of computing
resources, which depends on the number of items being processed. Complex
algorithms can have a significant overhead that should be considered.
• Operations that can be used to analyze a sorting process and whose count can
be used to measure overall efficiency of the algorithm:
• comparing two items to see which is smaller (the most common way)
• exchanging two items into a different position (costly operation)
• The total number of comparisons is the sum of the first n-1 integers. Recall
that this is given by (n-1)*n/2. This is O(n^2) comparisons.
• In the best case, if the list is already ordered, no exchanges will be made.
However, in the worst case, every comparison will cause an exchange. On
average, we exchange half of the time.
• The number of comparisons for a selection sort is the sum of the first
n-1 integers. It is O(n^2), like bubble sort.
• However, selection sort performs fewer exchanges, and typically
executes faster in benchmark studies.
return right_mark
when the marks have stopped, both
items are in the wrong side;
DESIGN OF ALGORITHMS USING PYTHON 2023/24
so they are switched
Quick Sort - analysis
• There is no need for additional memory as in the merge sort process.
• If the partition always occurs in the middle of the list:
• there will again be log n divisions
• in order to find the split point, each of the n items is checked against the
pivot.
• The result is n log n.
• In the worst case, the split points may not be in the middle and can be very
skewed:
• sorting a list of n items divides into sorting a list of 0 and a list of n-1 items…
• The result is an O(n2) sort with all of the overhead that recursion requires.