L3 Searching & Sorting algorithms
L3 Searching & Sorting algorithms
Insertion
Searching
Searching is when we find something in a data structure. We
frequently search for strings in things like web pages, PDFs,
documents, etc., but we can also search through other data
structures, like lists, dictionaries, etc.
Depending on how our data is organized, we can search
in different ways. For unorganized data, we usually have
to do a linear search, which is the first type of search we
will discuss.
If our data is organized in some way, we can do more
efficient searches. If our data is in a strict order, we can
perform a binary search, which is the second type
3
of search
we will look at.
Linear Searching
The most straightforward type of search is the linear
search.
We traverse the data structure (e.g., a string's characters,
or a list) until we find the result. How would we do a linear
search on a list, like this? Let's say we are searching for 15.
lst = [12, 4, 9, 18, 53, 82, 15, 99, 98, 14, 11]
8
Linear Searching
The most straightforward type of search is the linear
search.
We traverse the data structure (e.g., a string's characters,
or a list) until we find the result. How would we do a linear
search on a list, like this? Let's say we are searching for 15.
lst = [12, 4, 9, 18, 53, 82, 15, 99, 98, 14, 11]
9
Binary Search C
code
Binary Search Animation
Binary Searching
Can we calculate the maximum number of guesses a binary
search will take? Sure!
Each guess splits the amount of the list we have to search
by a factor of 2. Let's see what this means for 16 elements,
if we guess incorrectly until we reach one element. The
number of elements left will drop by half each time:
16 -> 8 -> 4 ->2 ->1
This was four total divisions (16/2 = 8, 8/2=4, 4/2=2, 2/2
= 1), so we have: 4
1
16 × ( ) = 1
2
In general, this
becomes:
1 where n is the number of elements, and
n× ( ) k
2 = 1 knumber
is the
of times we have to 15
divide.
Binary Searching
We can
simplify:
k
1 1
n× ( ) = 1 →n× k = 1→n=
2 2
2k
We can take the log2 of both sides to get:
k = log2 (n)
Therefore, the maximum number of steps, k, for a
binary search is log2(n).
16
18
Bubble sort C
code
Bubble sort C
code
Selection Sort
24
Selection Sort
Algorithm
25
Selection Sort
Algorithm
26
Selection Sort
Algorithm
27
Selection Sort
2
Selection sort is always an n algorithm, because you don't get the benefit
if
the list is already sorted you always have to find the next smallest
element.
What might selection sort be good for?
If you want to find the "top X" elements, you can do so relatively
fast (at least as fast as any other algorithm, anyway)
28
Selection Sort C
Code
29
Selection sort is also easy to write, but unless you want to find the top X,
it is worth it to simply write insertion sort, instead.
Selection Sort Animation
30
Selection sort is also easy to write, but unless you want to find the top X,
it is worth it to simply write insertion sort, instead.
Insertion Sort C
code
34
Insertion Sort C
code
35
Insertion Sort
Animation
Merge Sort
So far, we have seen sorts that have bad worstcase performance (and in the
case of selection sort, always have bad performance).
Now, we are going to look at a sort called merge sort that has good
performance, always.
Merge sort uses the same basic idea as binary search: split the problem
into halves as much as you can, leading to logarithmic behavior. Merge
sort is called a divideandconquer algorithm because of this splitting.
Merge sort can be coded recursively, so we will look at that.
The basic idea of merge sort is to sort already sorted lists. For
example: lst1 = [3, 5, 11]
lst2 = [1, 8, 10]
merge(lst1, lst2) produces [1, 3, 5, 8, 10, 11]
37
Merge Sort Animation
Merge Sort C
Code
Merge Sort C
Code
Merge Sort C
Code
Merge Sort Animation
Quick Sort
Quicksort is a sorting algorithm that is often faster than most other types of sorts,
including merge sort
However, though it is often fast, it is not always fast and can degrade significantly
given the wrong conditions
Quicksort is another divide-and-conquer algorithm
The basic idea:
Divide a list into two smaller sublists: the low elements and the high elements.
Then, recursively sort the sublists
44
Quick Sort
The quicksort algorithm:
Pick an element, called a pivot, from the list
Reorder the list so that all elements with values less than the pivot
come before the pivot, while all elements with values greater than
the pivot come after it. After this partitioning, the pivot is in its final
position. This is called the partition operation.
Recursively apply the above steps to the sub-list of
elements with smaller values and separately to the sublist of elements
with greater values.
The base case of the recursion is for lists of 0 or 1 elements, which
do not need to be sorted.
45
Quick Sort
46
Quick Sort
47
Quick Sort
48
Quick Sort C
Code
Quick Sort C
Code
Quick Sort C
Code
Quick Sort
Animation
Time and Space Complexity of Sorting
Algorithm