... start recording
Any questions about anything?
Questions about homework? quicksort & bubblesort?
Claim: quicksort's worst behavior (if the pivots are chosen badly)
is O(n^2)
rather than O(n log(n))
. Give an example of this behavior
and what a "bad pivot" means in this case.
*
* *
* * * *
[ 1, 2, * 3, 4, || 5, 6, * 7, 8 ]
pivot : pick first number in partition
[8, 3, 4 , 12, 15 ] pick 8 as pivot : fine
[1, 2, 3, 4, 5, 6, 7, 8 ] pick 1 as pivot : not good
1
/ \
2
/ \
3
/ \
4
pick first index, last index, middle index,
choose the middle for pivot
This is a general issue with the divide-and-conquer approach : if your "divide" doesn't, then you don't conquer.
In breakout rooms, see if you can implement the selection sort in C using the attached template sorting_template.c , (similar to what I posted last week), working in pairs. Then we'll come back together and discuss.
You can find my version in code/sorting
Let's start to discuss "binary trees" , "priority queues", "heaps", and "binary search trees" .
The runestone pythonds binary heap notes are a good resource for these ideas, with pictures and python code.
Also see the wikipedia: binary tree page.
Let's see if we can adopt the pythonds code and write a python ADT heap, at least the basic operations :
- create empty heap
- add additional element
- pop min (or max)
The essential ideas are :
O(log n)
.Note that this heap is not the only sort of binary tree; "binary search" and "binary search trees" are organized differently and used for a different purpose (searching for things).
We'll continue this on Thursday ; the python code that I started (which I've named node_v0.py, for "version 0") is attached.
last modified | size | ||
heap_v0.py | Mon Mar 22 2021 05:33 pm | 1.4K | |
sorting_template.c | Mon Mar 22 2021 02:13 pm | 3.4K |