Your browser doesn't support the features required by impress.js, so you are presented with a simplified version of this presentation.

For the best experience please use the latest Chrome, Safari or Firefox browser.

CIS 115

Lecture 7: Algorithms

al-Khwārizmī

Abū ʿAbdallāh Muḥammad ibn Mūsā al-Khwārizmī

Image Source: Wikipedia

Algorithm

A finite list of specific instructions for carrying out a procedure or solving a problem

Euclid

Image Source: Wikipedia

Euclid's Algorithm (GCD)

  1. Start with 2 numbers, A and B
  2. If either one is zero, the answer is the other number
  3. Subtract the smaller number from the larger number
  4. Repeat steps 2 - 4 until an answer is found

Example: GCD of 1071 & 462

Example: GCD of 1071 & 462

Sorting Algorithms

Insertion Sort

Image Source: Wikipedia

  1. Choose an element from the source
  2. Place it in the correct place in the destination
  3. Repeat until source is emtpy

Bubble Sort

Image Source: Wikipedia

  1. Compare the first two elements
  2. If they are out order, swap them
  3. Move one element over and repeat
  4. When the end is reached, start over
  5. Continue until no more swaps are made

Big O Notation

Image Source: Wikipedia

Big O Notation

Bubble Sort - Worst Case?

Bubble Sort - Worst Case?

Bubble Sort - Worst Case?

Sorting Algorithms

Merge Sort

Image Source: Wikipedia

Merge Sort

  1. Split the items into two halves
  2. Repeat step 1 until each has 1 item
  3. Choose 2 parts and merge them together by choosing the smallest item repeatedly from the front of each part
  4. Continue merging parts together until no more remain

Quicksort

Image Source: Wikipedia

Quicksort

  1. Choose an item from the list as a "pivot"
  2. Put all items less than that item to its left, and put all items greater to its right
  3. Repeat these steps for the items on each side of the pivot.

Sorting Algorithms

Everyday Heuristics

Travelling Salesman Problem

Image Source: Wikipedia

Travelling Salesman Problem: Algorithms

Travelling Salesman Problem: Heuristic

Nearest Neighbor (Greedy Algorithm)

Travelling Salesman Problem

Greedy Solution: 67 miles

Image Source: Wikipedia

Travelling Salesman Problem

Optimal Solution: 62 miles

Image Source: Wikipedia

Travelling Salesman Problem: Heuristic

Nearest Neighbor (Greedy Algorithm) - O(d n)


Time can vary widely based on how the data is presented and sorted

Assignments

Blog 3: Algorithms

Think about something that you do every day. That one thing probably is composed of many smaller steps, which you have to perform in the correct order. How would you describe those steps to someone unfamiliar with the action? How would you describe them to a robot that can follow your actions? While we may not think of it in this way very often, most of our daily lives could be expressed as an algorithm. Choose a few examples of actions you perform often, and write about how you would express them as algorithms. Some things to consider:

Bubble Sort

Image Source: Wikipedia

  1. Compare the first two elements
  2. If they are out order, swap them
  3. Move one element over and repeat
  4. When the end is reached, start over
  5. Repeat these steps as many times as the number of items in the list