Any questions?

Today's topics:

- Go over the homework:
- reversing a linked list
- bubblesort (which we have talked about)
- quicksort (which we not yet done in detail)

- Start to discuss binary trees and the "heap" data structure.

I've posted my answers, which we'll work through, particularly quicksort.

Note that quicksort is actually a family of algorithms, not just one - there are various choices of how to choose a pivot, and in how to rearrange the array to put smaller values to the left of the pivot and larger values to the right.

It's claimed that "quicksort's worst behavior if the pivots are chosen badly
is `O(n^2)`

rather than `O(n log(n))`

. Let's discuss this,
and understand what a "bad pivot" means in this context.

```
*
* *
* * * *
[ 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.

Next 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 :

- it's a binary tree, with each node smaller (or bigger) than its children
- it's always "complete", i.e. filled in top-to-bottom and left-to-right (avoids being unbalanced)
- we can store it in a regular indexed array (with an indexing trick)
- we can add or remove elements by swaps that are at most the height of the tree,
`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).

Note also that python includes a built-in library which implements this priority queue, which it calls heapq.

I've attached some starting python code (heap_v0.py), which we'll build upon with some live coding, working towards implementing and testing a heap.

https://cs.bennington.college /courses /spring2022 /algorithms /notes /march21

last modified Sun March 20 2022 11:14 pm

last modified Sun March 20 2022 11:14 pm

last modified | size | ||

heap_v0.py |
Sun Mar 20 2022 11:13 pm | 1.7K |