0% found this document useful (0 votes)
4 views26 pages

19 Sorting and Complexity

The document outlines a presentation on embedded programming focused on searching and sorting algorithms using Python, specifically the Insertion Sort method. It covers algorithm efficiency, Big O notation, and includes exercises for students to implement sorting algorithms and test their code. Additionally, it emphasizes the importance of functional and non-functional requirements, architectural design, and thorough testing through unit tests and code reviews.

Uploaded by

calibwong.11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views26 pages

19 Sorting and Complexity

The document outlines a presentation on embedded programming focused on searching and sorting algorithms using Python, specifically the Insertion Sort method. It covers algorithm efficiency, Big O notation, and includes exercises for students to implement sorting algorithms and test their code. Additionally, it emphasizes the importance of functional and non-functional requirements, architectural design, and thorough testing through unit tests and code reviews.

Uploaded by

calibwong.11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

CSI4103 – Embedded Programming

Searching and Sorting with Python

Presented by: David Knox

Faculté de génie | Faculty of Engineering


uOttawa.ca

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

return end_time - start_time # elapsed time for sort

list_sizes = [100, 500, 1000, 2000, 5000]


# Measure sorting time for each list size
sorting_times = [measure_sorting_time(size) for size in list_sizes]

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]

– expression: The operation or value to be included in the new


list. This expression is applied to each item in the iterable.
– item: The variable that takes each value from the iterable.
– iterable: The existing iterable (list, tuple, range, etc.) that
provides the values for the list comprehension.
– condition (optional): An optional condition that filters which
items are included in the new list. The expression is applied only if
the condition evaluates to True

sorting_times = [measure_sorting_time(size) for size in list_sizes]

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)

for i in range(1, n):


key = arr[i]
# Move elements of arr[0..i-1] greater than the current
# “key” element to one position ahead of their current position
j = i - 1
while j >= 0 and key < arr[j]:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
return None

list = [1,3,4,3,65,7,4,23,6,76,5,4,76,57,54,4,999]
print(insertion_sort(list))

• Let us visualize this code using a tool too:


– https://pythontutor.com/visualize.html#mode=edit
• Try it out yourself, until you understand how the code
works! Q: What will it print?
genie.uOttawa.ca | engineering.uOttawa.ca
For a python example: https://www.youtube.com/watch?v=Nkw6Jg_Gi4w
Insertion Sort Complexity
def insertion_sort(arr):
n = len(arr)

for i in range(1, n):


key = arr[i]
# Move elements of arr[0..i-1] greater than the current
# “key” element one position ahead of their current position
j = i - 1
while j >= 0 and key < arr[j]:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
• Nested loops!
– Outer loop executes n times (exactly!) and
– Inner loop executes 0, 1, 2, …, n times
– Total loop count = n times something that gets as high as n
– Let’s call it n, too, since it varies with the input size and is not constant
• Q: What would the the Big O complexity be, if there was
another for loop inside the while loop?
genie.uOttawa.ca | engineering.uOttawa.ca
Simple
Exercise
genie.uOttawa.ca | engineering.uOttawa.ca
Exercise - Sorting a List (your turn)
• Sort a list of 10 items (create list with random set of values in a
random order and then write code to sort that list)
• There are a few different sorting algorithms (e.g. bubble sort,
selection sort, insertion sort, heap sort, merge sort, etc.)
• Implement selection sort (or another sorting algorithm)
– Make a copy of your starting list before changing values (lists are
mutable, which means that their entries can be changed, without
creating a brand-new python object) to let you see the change
– Use logical variable names
– Add comments that show your intent
– “Profile” the execution time of your code for a few different list sizes
(random entries) and plot the results using matplotlib.
Q: Are the results what you expected (i.e. based on Big O analysis)?
– Write some test cases and estimate your test coverage
genie.uOttawa.ca | engineering.uOttawa.ca
Basic Design
and Test
Process
genie.uOttawa.ca | engineering.uOttawa.ca
Functional and Non-Functional
Requirements
• Document, briefly and succinctly, the
functional requirements
• Document, briefly and succinctly, any non-
functional requirements

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

Define basic functional testing use cases (go/no go


testing)

Determine “edge” or “corner” cases (e.g. beginnings


and ends of loop conditions or specific error cases)

def neg_function (end_value, list):


for i in range(0, end_value):
try:
list[i] = -1*list[i]
except:
print(“problems!”)
return -1
return None
# edge cases for this function?

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

You might also like