Algorithms
and
Data
Structures

Spring 2021
course
site
-->

Mon March 22

... 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.

practice with C and selection sort

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

work through priority queue & heapsort

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 :

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.


https://cs.bennington.college /courses /spring2021 /algorithms /notes /sorting3
last modified Mon March 22 2021 5:34 pm

attachments [paper clip]

  last modified size
TXT heap_v0.py Mon Mar 22 2021 05:33 pm 1.4K
TXT sorting_template.c Mon Mar 22 2021 02:13 pm 3.4K