Lecture 9 Simple Sorting Algorithms
Lecture 9 Simple Sorting Algorithms
1
Learning Outcomes
At the end of the next few lectures, a student will be able to:
describe the behaviour of and implement simple sorting
algorithms:
selection sort
insertion sort
describe the behaviour of and implement more efficient sorting
algorithms:
quick sort
merge sort
analyze the best, worst, and average case running time (and
2 space) of these sorting algorithms
Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University
Last Lecture
We saw how to …
Describe Queue
Define public interface of Queue ADT
Design and implement Queue ADT using various data
structures (CDTs)
Compare and contrast these various implementations
using Big O notation
Give examples of real-life applications (problems) where
we could use Queue to solve the problem
3 Solve problems using Queue ADT
Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University
Today’s menu
a. 15
b. 5
c. 9
d. 12
e. None of the above
5
a. 2
b. 23
c. 89
d. 100
e. None of the above
a. 2
b. 23
c. 89
d. 100
e. None of the above
a. 15
b. 5
c. 9
d. 12
e. None of the above
if there is one
10
Linear search
Time efficiency of worst case scenario: O(n)
Space efficiency: O(1)
Binary search
Time efficiency of worst case scenario: O(log2 n)
Space efficiency: O(1)
11
Binary Search vs Linear Search
Advantages:
1. Binary search is much faster than linear search especially
when searching large data because it does not have to
look at every element (at every iteration, it ignores ½ of
data being searched).
Disadvantages:
1. A bit more complicated to implement and test.
2. Data structure must be sorted.
Great if data is already sorted, but if this is not the case …
14
How Selection Sort works
16
17 The end:
Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University
Time Efficiency Analysis of Selection Sort
19
22
How Insertion Sort works
Array has n elements
At the start, insertion sort considers the first element of the array to be
already sorted -> sorted section
Starts with the second element (at index 1)
Repeat until the array is sorted
1. Pick the first element of the unsorted section and store it in a temp
variable
2. Comparing it with each element of the sorted section (starting with
the last element and moving towards the first element), looking for
So, the where in this sorted section it should be placed (i.e., in its proper sort
actual sorting order)
is done when 3. Shift the elements in the sorted section up one position (starting with
we insert the last element of sorted section all the way down to the desired
an element.
23 place) to make space for this element (if needed)
4. Insert it in this newly vacated place in the sorted section
Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University
Space Efficiency
Analysis
Insertion Sort is an in place algorithm
in-place: algorithm does not require additional space i.e.,
another array(s) aside from the original array
Insertion sort starts with a sorted section of 1 element:
The start:
As the array is being sorted, the unsorted section decreases
and the sorted section increases:
…
24
The end:
Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University
Demo - Let’s have a look at Insertion Sort
25
Activity:
How should the array be organized in order to achieve
the best case scenario of the time efficiency of insertion
sort?
27
Give an example of such array:
Activity:
How should the array be organized in order to
achieve the worst case scenario of the time
efficiency of insertion sort?
Give an example of such array:
29
Summary – Insertion Sort Algorithm
Time efficiency
Best case scenario: O(n)
Worst case scenario: O(n2)
Average case scenario: O(n2)
Space efficiency
in-place sorting algorithm => O(1)
30
31
Source: https://www.baeldung.com/cs/stable-sorting-algorithms
Stability - Example from wiki
Here is an example of stability of sorting algorithm using
playing cards. When the cards are sorted by rank using a
stable sorting algorithm, the two 5’s remain in the same order
(in relation to each other) in the sorted output as they were in
originally. When they are sorted using a non-stable sorting
algorithm, the 5’s may end up in the opposite order (in
relation to each other) in the sorted output:
32
Source: https://en.wikipedia.org/wiki/Sorting_algorithm
When does stability matter?
Data sorted using a stable sort algorithm:
33
Source: https://www.freecodecamp.org/news/stability-in-sorting-algorithms-a-treatment-of-equality-fa3140a5a539/
Conclusion – Simple Sorting Algorithms
Insertion sort versus Selection sort
Efficient: for small n’s
More efficient in practice than most other simple
quadratic (i.e., O(n2)) algorithms
Stable: does not change the relative order of elements
with equal keys
Both sorting algorithms: Selection sort and Insertion sort
In-place: only requires a constant amount of additional
memory space, i.e., O(1)
Both are from a class of sorting algorithm called
34 Comparison sort
Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University
√ Learning Check
Quick Review of Searching algorithms
Sorting
Selection sort
Insertion sort
Analyze their best, worst, and average case running time
and space efficiency of these sorting algorithms
35
36