Skip to main content

CSC 151 2019S, Class 35: An introduction to sorting

Overview

  • Preliminaries
    • Notes and news
    • Upcoming work
    • Extra credit
    • Questions
  • The problem of sorting
  • Writing sorting algorithms
  • Some examples
  • Formalizing the problem

Preliminaries

News / Etc.

  • I brought food-like substances.
  • Mentor sessions Thursday 7-8 p.m., Thursday 8-9 p.m.
  • Today is a “talk day” rather than a lab day.
  • Congrats to your classmate who tied for second in the Kinetic Sculpture Competition!
  • Welcome to any prospective students. Good luck on your decisions!

Upcoming work

  • Project due Tuesday!
    • Rubric available for your perusal.
    • Send me code, instructions for running, and report.
    • The parts of the report are specified in the rubric. I’d expect an introdution that describes the problem, a description of the key algorithms you wrote, a description of what you did with the results of those algorithms, and some conclusions about what you might have learned. Four sections, 1-2 pargraphs each.
  • Reading for Wednesday: Algorithms for sorting lists and vectors
  • No lab writeup
  • Friday’s quiz: Searching and sorting
  • Flash cards Wednesday: Searching and sorting
    • Our last flash cards!
  • Exam 3 due next Tuesday.
    • It is not “officially” released until this Wednesday; I still have time to correct typos and infelicities.
    • Prologue due Friday night.

Extra Credit

I would certainly appreciate suggestions of other extra credit activities (preferably via email).

Extra credit (Academic/Artistic)

  • Tomorrow: CS Table, Tuesday, Facebook Data, Noon, Middle PDR
  • Three talks by Prof. Dr. Yvonne Foerster (https://yvonnefoerster.com/)
    • Wednesday: May 1, 4:30-6pm, HSSC S3325: Beyond the Anthropocene: Technology, Innovation, and the (Post-)Human Condition
    • Thursday, May 2, Noon-12:50pm, HSSC N3110 Degrees of Freedom: Embodiment, Neuroplasticity, and the Need for a Critical Neuroscience (Lunch and beverages provided)
    • Friday, May 3, Noon-12:50pm, Bucksbaum 152: Designing Future Bodies: Fashion and Technology (Lunch and beverages provided)

Extra credit (Peer)

  • New: Sunday, 2pm, Herrick: Singers vs. Orchestra

Extra credit (Wellness)

Extra credit (Wellness, Regular)

  • Today: 30 Minutes of Mindfulness at SHAW every Monday 4:15-4:45
  • Any organized exercise.
  • 60 minutes of some solitary self-care activities that are unrelated to academics or work. Your email reflection must explain how the activity contributed to your wellness.
  • 60 minutes of some shared self-care activity with friends. Your email reflection must explain how the activity contributed to your wellness.

Extra credit (Misc)

Other good things

  • The Grinnellian: Student concert, next Saturday.

Questions

The problem of sorting

  • We like to sort; it is useful to have things in order.
    • It makes binary search easier.
    • It makes our remove-duplicates algorithm much faster (or at least easier).
    • A really inefficient way to do max or min. (Nicer if you need like the first three or last three or ….)
    • Can help with hash tables.
  • Writing sorting algorithms is therefore an important task for computer scientists.
  • That will be our task for the next few days.

Writing sorting algorithms

  • Look at similar algorithms we’ve written in the past and adapt.
  • Do things by hand and then try to generalize.

Some examples

Insertion sort

  • Split our world into sorted and unsorted.
  • Initially, the first element is sorted and everything else is unsorted.
  • Repeatedly take the first element of unsorted and put it in the correct place in sorted.

Radix

  • Put all the A’s together
  • Put all the B’s together
  • Put all the C’s together
  • Put all the D’s together
  • In each group, do the same thing with the second letter
  • And so on and so forth
  • Can improve with knowledge of the distribution

Radix plus insertion

  • Divide into three groups, smaller, middle, larger. Then run insertion.

Selection

  • Conceptually we have two lists: sorted and unsorted
  • Repeatedly find the smallest thing in unsorted and add it to the end of sorted.

Timsort will come on Friday

Clownsort / Permutation sort

  • Randomly permute the list
  • See if it’s sorted
  • If so, you’re done
  • If not, start over

Parallel bubble sort

  • Compare to the person next to you (people in odd positions to people in even positions) and swap if out of order.

Machine learning sort

  • Give the problem to a machine learning algorithm
  • Use what it comes up with

Analysis

Insertion sort

  • Putting the first thing in the new list costs us about 1.
  • Putting the second thing the new list costs us about 2 (in the worst case)
  • Putting the third thing the new list costs us about 3 (in the worst case)
  • Worst case 1+2+3+4+…+n. = n*(n+1)/2. An “n squared” algorithm
  • Idea: Instead of comparing to neighboring elements, use binary search to find the location.
    • For binary search to work, things must be sorted.
    • For binary search to work, they must be in an array.
  • Once we find where it goes, we have to shift things over.

Note: With searching, we found that it was useful to think about limiting the form of the values (sortted) and using a “divide and conquer” algorithm.

Improving sorting algorithms

We’ve seen that divide-and-conquer was a succesful strategy for searching; can we use it for sorting?

  • Divide the list in half (how?)
  • Do something with each half (what?)
  • Put them back together (how?)

Mergesort

  • Divide the list into two halves, positionally
  • Sort each half (using recursion)
  • Merge the two together, by repeatedly grabbing the smaller of the first remaining element in each half.

Median sort

  • Divide the list into those less than the median and greater than the median
  • Sort the two halves
  • Append together

Quicksort

  • Median sort, using a randomly chosen median
  • Base case: 1 element

Formalizing the problem