19 Sorting and Complexity
19 Sorting and Complexity
Image: https://www.parcelandpostaltechnologyinternational.com/news/automation/mapping-optimizes-sorting-centers.html
Housekeeping
• ITP Metrics Peer Feedback Survey (pair-based
project)
• Start your group-based project work now!
– Functional blocks and work assignments
– Do not leave this until the day of your demonstration
or until exams and end-of-term assignments start to
pile up (it is worth 45% of your project grade)
– Time allotted in the remaining labs, but it won’t be
enough for debugged code. Make sure that you use
the lab just for testing your already-unit-tested
code… USE GITHUB for code management!
genie.uOttawa.ca | engineering.uOttawa.ca
Agenda
• Insertion Sort
• Code Efficiency
• Big O Notation
• Exercise – Insertion Sort (your turn!)
• Embedded Software Testing
• (Simple?) Exercises
genie.uOttawa.ca | engineering.uOttawa.ca
Insertion
Sort
genie.uOttawa.ca | engineering.uOttawa.ca
Insertion Sort – High
Level Algorithm
• Algorithm: Process or method to
solve a problem (e.g. instruction
sequence)
– Example of an algorithm: Insertion
sort repeatedly inserts elements into
the right spot of a previously-sorted
sublist, shifting elements as required.
• At each iteration, insertion sort
removes one element from the
input data, finds the location it
belongs within the sorted list, and
inserts it there. It repeats until no
input elements remain.
genie.uOttawa.ca | engineering.uOttawa.ca
https://en.wikipedia.org/wiki/Insertion_sort
Visualization of Insertion Sort
genie.uOttawa.ca | engineering.uOttawa.ca
Insertion Sort – Implementation
• Sorting is typically done in-place, by iterating up the
array/list, growing the sorted list behind it.
• At each array/list-position, it checks the value there
against the largest value in the sorted list (which
happens to be next to it, in the previous array-
position checked, when sorting in-place!)
– If larger, then it leaves the element in place and moves to
the next element for checking
– If smaller, it finds the correct position within the sorted list,
shifts all the larger values up to make a space, and inserts
the new value into the correct position
genie.uOttawa.ca | engineering.uOttawa.ca
https://en.wikipedia.org/wiki/Insertion_sort
Code
Efficiency
genie.uOttawa.ca | engineering.uOttawa.ca
Algorithm Efficiency
• Usually refers to the run time efficiency of an algorithm
– We can also talk about memory efficiency of algorithms
• Q: Is Insertion sort the most efficient sorting method? A: No!
• Q: How long would it take to sort a list?
• Q: What does your answer depend upon?
– We can measure the actual time taken with the time.time() function!
• Q: How could you measure the efficiency in a way that would
not depend on your particular computer or processor speed,
etc. to allow different sorting algorithms to be compared in a
‘fair’ way, based on your previous answers?
• A: Big O notation!
genie.uOttawa.ca | engineering.uOttawa.ca
Measuring Sorting Time
import random
def insertion_sort(arr):
for i in range(1, len(arr)):
key = arr[i] # current key element being examined
j = i - 1
while j >= 0 and key < arr[j]: # find point to insert in
arr[j + 1] = arr[j] # shuffle values over to make room for new one
j -= 1
arr[j + 1] = key # insert the new item in the right place
def measure_sorting_time(list_size):
data = [random.randint(1, 1000) for _ in range(list_size)] # generate random data
start_time = time.time() # record the start time (ms)
insertion_sort(data) # do the sort
end_time = time.time() # record when the sort was completed
genie.uOttawa.ca | engineering.uOttawa.ca
Python List Comprehension
• List comprehensions are often used to replace simple
for loops, when creating lists
new_list = [expression for item in iterable if condition]
genie.uOttawa.ca | engineering.uOttawa.ca
Big O
Notation
genie.uOttawa.ca | engineering.uOttawa.ca
“Big O” Notation
• “Big O” shows the relationship between an
algorithm’s input size and the complexity (i.e.
number of operations required) to complete the
algorithm, without considering things that are
constant with respect to that input size
– We say that insertion sort has O(n2) complexity (i.e. the
capital letter “O” followed by something in parentheses,
that tells us what we need to know)
– Q: What is n, for an insertion sort (i.e. what is the
measure of the input ‘size’ for a sorting algorithm)?
– Q: Why is it O(n2)? … Let’s see!
genie.uOttawa.ca | engineering.uOttawa.ca
https://en.wikipedia.org/wiki/Big_O_notation
Insertion Sort Visualization
def insertion_sort(arr):
n = len(arr)
list = [1,3,4,3,65,7,4,23,6,76,5,4,76,57,54,4,999]
print(insertion_sort(list))
genie.uOttawa.ca | engineering.uOttawa.ca
Architectural Design
• Draw a block diagram of your code, showing
(high-level) information flows for each
interface
– Identify any risk areas (e.g. data structure size,
execution time, complexity, other)
• Define and document specific modules or
libraries being re-used, showing the major
elements of information flows for each
– Assess “support” or popularity and “quality” for
each of these and note any specific risks
genie.uOttawa.ca | engineering.uOttawa.ca
Use Cases and Corner Cases
genie.uOttawa.ca | engineering.uOttawa.ca
Test your code using unittest
• Write at least ten test cases for your sorting
code
• Determine your test coverage and improve it
until it rises to 100% (or so!)
• Find at least three bugs with your unit tests…
genie.uOttawa.ca | engineering.uOttawa.ca
Pair Review Code
• Do a code walk-through with your partner
– Review each other’s code
– Review unit testing and unit test cases for
functional completeness/adequacy and test
coverage
– Find more bugs!
genie.uOttawa.ca | engineering.uOttawa.ca
Functional Tests
• Review your functional block diagram for the
different blocks and their inputs and outputs
• Define a logical sequence of tests, in divide-and-
conquer fashion, that requires only one new
(independent) block to work at a time
– These tests can build upon each other, as functionality is
tested as being perfectly functional
• Think about “edge” or “corner” cases
– Handle all error cases in your code and make sure that you
“exercise” each of these cases in your tests
– If debugging on the target, print statements and use of
LEDs can be useful for monitoring purposes!
genie.uOttawa.ca | engineering.uOttawa.ca
Backup
Exercise
genie.uOttawa.ca | engineering.uOttawa.ca
Backup Exercise – Functional Testing
• Define a logical series of functional tests for the
pair-based project (i.e. dual-Pico PWM monitoring)
– Q: What are the individual tests?
– Q: What order would you use to do the testing?
– Q: How will you do the testing (i.e. using Thonny or VS
Code or directly on the target)?]
– Q: How will you know that the results are a pass or a fail
– Q: What are the “corner” or “edge” cases that you need
to test?
– Q: What are some other potential bugs in your code?
Use a pair-based code walkthrough method for this!
genie.uOttawa.ca | engineering.uOttawa.ca