Programming: Algorithms

    Programming: Algorithms

      Let's talk about something verging into the realm of too much information: brushing your teeth. No, we have no idea whether you brushed your teeth this morning (though we bet the people around you can guess). What's interesting about brushing your teeth is the fact that it doesn't take a lot of…thought.

      That's a good thing; Shmoop isn't really a morning person.

      You wake up; you roll out of bed (sometimes literally). After slapping on some clothes you thought looked good (according to your sleep-addled mind) and head to the bathroom to do the one thing you can't really mess up. Why? All the steps have been memorized.

      1. Get toothpaste. 
      2. Put toothpaste on toothbrush. Or sometimes, after an unfortunate incident with a dog and a toilet bowl, put the toothpaste straight on your finger. 
      3. Rub toothbrush (or toothpasted finger) on teeth—preferably yours. 
      4. Wash hands (or if you're running really late after losing a battle with your dog and the toilet, just wipe hands on pants to remove excess toothpaste and slobber).

      Brushing your teeth might not feel like anything impressive, but it's secretly [dramatic pause] an algorithm.

      Algorithms are logically sequenced steps that solve specific problems. You can’t do step four before step two. Usually, you want to write them in a human language and work out the kinks before you start thinking in logic. Trust us, it makes life a lot easier.

      To write a good algorithm you need a base case, an end case, and a set of instructions to get you from base case to end case.

      Confused yet?

      Let's revisit brushing your teeth. We swear: we aren't trying to hygiene-shame you…probably.

      Preconditions: must have toothbrush and toothpaste, must have teeth

      Start State: a set of unbrushed teeth

      End State: a set of…brushed teeth

      Steps:

      1. Apply toothpaste to brush.
      2. Brush teeth in an up-down motion for about two minutes.
      3. Rinse mouth of toothpaste.
      4. Clean toothbrush.

      Any time you solve an algorithm using a set number of steps, you're using an algorithm. It's really that simple. Without algorithms, computer science probably wouldn't exist.

      Oh shoot, we didn't brush our teeth today.