Lecture # 2 Algorithms and Programs
Today Questions: From Homework #1 1.Introduce: What is an Algorithm? 2.Explain: Algorithm defined 3.Demo: Examples of Algorithms 4.Practice: You will create an Algorithm 5.Evaluate: We will share and evaluate our Algorithms 6.Re-practice: Analyze your favorite recipe as an Algorithm We will look at a classical sorting Algorithm together You will look at a Program on your own
Review of Homework # 1 Groups share Sorting Algorithms What is the Key Idea of the Algorithm? What makes an algorithm efficient? What is the best Sorting Algorithm?
Today Questions: From Homework #1 1.Introduce: What is an Algorithm? 2.Explain: Algorithm defined 3.Demo: Examples of Algorithms 4.Practice: You will create an Algorithm 5.Evaluate: We will share and evaluate our Algorithms 6.Re-practice: Analyze your favorite recipe as an Algorithm We will look at a classical sorting Algorithm together You will look at a Program on your own
Algorithms What is an Algorithm? A precise, repeatable method or finite sequence of steps to solve a problem or accomplish a task What are some examples of algorithms? - In everyday life? - In Science? - In Computing? - In the workplace?
Algorithms What is an Algorithm? A precise, repeatable method or finite sequence of steps to solve a problem or accomplish a task What are some examples of algorithms? - In everyday life? Recipes – Dressing - Driving - In Science? Temperature Conversion - Adding - In Computing? Searching – Network Traffic - In the workplace? Purchasing – Food Preparation
Algorithms Example 1 To Die-For Pot Roast: Ingredients: 1 (4 -5 lb) beef roast, any kind 1 (1 1/4 ounce) package brown gravy mix, dry 1 (1 1/4 ounce) package dried Italian salad dressing mix 1 (1 1/4 ounce) package ranch dressing mix, dry 1/2 cup water
Algorithms Example 1 (cont.) To Die-For Pot Roast: Directions: Step 1: Place beef roast in crock pot. Step 2: Mix the dried mixes together in a bowl and sprinkle over the roast. Step 3: Pour the water around the roast. Step 4: Cook on low for 7-9 hours.
Today Questions: From Homework #1 1.Introduce: What is an Algorithm? 2.Explain: Algorithm defined 3.Demo: Examples of Algorithms 4.Practice: You will create an Algorithm 5.Evaluate: We will share and evaluate our Algorithms 6.Re-practice: Analyze your favorite recipe as an Algorithm We will look at a classical sorting Algorithm together You will look at a Program on your own.
Algorithm Exercise Algorithm for Getting to School – write it on your laptop so you can share it Step 1: Unsleep (must be specific – when?) Step 2: Step 3: Step 4: Step 5: Step 6: Step 7:
What’s in Your Algorithm? WPbasic – on screen Share and Evaluate
Algorithm Efficiency What makes an algorithm efficient? - Is it the time that it takes? - Is it the number of steps required? - Is it the complexity?
Algorithm Efficiency What has been the decrease in the mile run over the past 5 decades? What has been the decrease in the amount of time required by a computer to add two numbers over the past 5 decades? Has the algorithm for addition changed?
What are the Essential Ingredients of an Algorithm? Input: Define type, format of input data Output: Define output/result Steps: Specific sequence of steps or instructions Computable: steps can be performed, validated Halts: The algorithm will halt in finite time
Today Questions: From Homework #1 1.Introduce: What is an Algorithm? 2.Explain: Algorithm defined 3.Demo: Examples of Algorithms 4.Practice: You will create an Algorithm 5.Evaluate: We will share and evaluate our Algorithms 6.Re-practice: Analyze your favorite recipe as an Algorithm We will look at a classical sorting Algorithm together You will look at a Program on your own
Exercise Determine the inputs, outputs, steps and stopping condition for your favorite recipe Determine the inputs, outputs, steps and stopping conditions for other everyday tasks How can you increase the efficiency of some of these tasks?
Today Questions: From Homework #1 1.Introduce: What is an Algorithm? 2.Explain: Algorithm defined 3.Demo: Examples of Algorithms 4.Practice: You will create an Algorithm 5.Evaluate: We will share and evaluate our Algorithms 6.Re-practice: Analyze your favorite recipe as an Algorithm We will look at a classical sorting Algorithm together You will look at a Program on your own
Programs Computer Programs are simply algorithms that are implemented in a particular programming language like java, javascript, HTML, C, python, etc. These Languages use a sequence of steps, named variables, assignment statements, and data types and data structures to implement the algorithm.
Program A sequence of things to do A = 75; B = A+13; A = A-B+3; C = A/2 + 1;
Program A sequence of things to do A = 75; B = A+13; A = A-B+3; C = A/2 + 1;
Program A sequence of things to do A = 75; B = A+13; A = A-B+3; C = A/2 + 1; A = 88
Program A sequence of things to do A = 75; B = A+13; A = A-B+3; C = A/2 + 1; A-B = -10
Program A sequence of things to do A = 75; B = A+13; A = A-B+3; C = A/2 + 1; A / / = -4
Today Questions: From Homework #1 1.Introduce: What is an Algorithm? 2.Explain: Algorithm defined 3.Demo: Examples of Algorithms 4.Practice: You will create an Algorithm 5.Evaluate: We will share and evaluate our Algorithms 6.Re-practice: Analyze your favorite recipe as an Algorithm We will look at a classical sorting Algorithm together You will look at a Program on your own
Exercise: Write a Sorting Algorithm Create an algorithm to sort (alphabetize) a list of names. How will you represent the names? How will you solve this problem? What will be the key idea of the algorithm? - now that you are an expert (of “Sorts”)
A Sort Algorithm Input: An unordered list of names Output:An ordered list of names Steps:1 Refer to the first two names as index A and B respectively 2If Name[A] > Name [B] swap Name[A] with Name [B] 3If B does not point to the last name on the list move B to the next name and go back to Step 2 4If there are 2 or more names after A move A to the next name move B to the name following A Go back to Step 2 Stop Name AJim BBill Alice Nancy Bob
A Sort Algorithm Input: An unordered list of names Output:An ordered list of names Steps:1 Refer to the first two names as index A and B respectively 2If Name[A] > Name [B] swap Name[A] with Name [B] 3If B does not point to the last name on the list move B to the next name and go back to Step 2 4If there are 2 or more names after A move A to the next name move B to the name following A Go back to Step 2 Stop Name AJim BBill Alice Nancy Bob
A Sort Algorithm Input: An unordered list of names Output:An ordered list of names Steps:1 Refer to the first two names as index A and B respectively 2If Name[A] > Name [B] swap Name[A] with Name [B] 3If B does not point to the last name on the list move B to the next name and go back to Step 2 4If there are 2 or more names after A move A to the next name move B to the name following A Go back to Step 2 Stop Name ABill BJim Alice Nancy Bob
A Sort Algorithm Input: An unordered list of names Output:An ordered list of names Steps:1 Refer to the first two names as index A and B respectively 2If Name[A] > Name [B] swap Name[A] with Name [B] 3If B does not point to the last name on the list move B to the next name and go back to Step 2 4If there are 2 or more names after A move A to the next name move B to the name following A Go back to Step 2 Stop Name ABill BJim Alice Nancy Bob
A Sort Algorithm Input: An unordered list of names Output:An ordered list of names Steps:1 Refer to the first two names as index A and B respectively 2If Name[A] > Name [B] swap Name[A] with Name [B] 3If B does not point to the last name on the list move B to the next name and go back to Step 2 4If there are 2 or more names after A move A to the next name move B to the name following A Go back to Step 2 Stop Name ABill Jim BAlice Nancy Bob
A Sort Algorithm Input: An unordered list of names Output:An ordered list of names Steps:1 Refer to the first two names as index A and B respectively 2If Name[A] > Name [B] swap Name[A] with Name [B] 3If B does not point to the last name on the list move B to the next name and go back to Step 2 4If there are 2 or more names after A move A to the next name move B to the name following A Go back to Step 2 Stop Name ABill Jim BAlice Nancy Bob
A Sort Algorithm Input: An unordered list of names Output:An ordered list of names Steps:1 Refer to the first two names as index A and B respectively 2If Name[A] > Name [B] swap Name[A] with Name [B] 3If B does not point to the last name on the list move B to the next name and go back to Step 2 4If there are 2 or more names after A move A to the next name move B to the name following A Go back to Step 2 Stop Name AAlice Jim BBill Nancy Bob
A Sort Algorithm Input: An unordered list of names Output:An ordered list of names Steps:1 Refer to the first two names as index A and B respectively 2If Name[A] > Name [B] swap Name[A] with Name [B] 3If B does not point to the last name on the list move B to the next name and go back to Step 2 4If there are 2 or more names after A move A to the next name move B to the name following A Go back to Step 2 Stop Name AAlice Jim BBill Nancy Bob
A Sort Algorithm Input: An unordered list of names Output:An ordered list of names Steps:1 Refer to the first two names as index A and B respectively 2If Name[A] > Name [B] swap Name[A] with Name [B] 3If B does not point to the last name on the list move B to the next name and go back to Step 2 4If there are 2 or more names after A move A to the next name move B to the name following A Go back to Step 2 Stop Name AAlice Jim Bill BNancy Bob
A Sort Algorithm Input: An unordered list of names Output:An ordered list of names Steps:1 Refer to the first two names as index A and B respectively 2If Name[A] > Name [B] swap Name[A] with Name [B] 3If B does not point to the last name on the list move B to the next name and go back to Step 2 4If there are 2 or more names after A move A to the next name move B to the name following A Go back to Step 2 Stop Name AAlice Jim Bill BNancy Bob
A Sort Algorithm Input: An unordered list of names Output:An ordered list of names Steps:1 Refer to the first two names as index A and B respectively 2If Name[A] > Name [B] swap Name[A] with Name [B] 3If B does not point to the last name on the list move B to the next name and go back to Step 2 4If there are 2 or more names after A move A to the next name move B to the name following A Go back to Step 2 Stop Name AAlice Jim Bill BNancy Bob
A Sort Algorithm Input: An unordered list of names Output:An ordered list of names Steps:1 Refer to the first two names as index A and B respectively 2If Name[A] > Name [B] swap Name[A] with Name [B] 3If B does not point to the last name on the list move B to the next name and go back to Step 2 4If there are 2 or more names after A move A to the next name move B to the name following A Go back to Step 2 Stop Name AAlice Jim Bill Nancy BBob
A Sort Algorithm Input: An unordered list of names Output:An ordered list of names Steps:1 Refer to the first two names as index A and B respectively 2If Name[A] > Name [B] swap Name[A] with Name [B] 3If B does not point to the last name on the list move B to the next name and go back to Step 2 4If there are 2 or more names after A move A to the next name move B to the name following A Go back to Step 2 Stop Name AAlice Jim Bill Nancy BBob
A Sort Algorithm Input: An unordered list of names Output:An ordered list of names Steps:1 Refer to the first two names as index A and B respectively 2If Name[A] > Name [B] swap Name[A] with Name [B] 3If B does not point to the last name on the list move B to the next name and go back to Step 2 4If there are 2 or more names after A move A to the next name move B to the name following A Go back to Step 2 Stop Name AAlice Jim Bill Nancy BBob
A Sort Algorithm Input: An unordered list of names Output:An ordered list of names Steps:1 Refer to the first two names as index A and B respectively 2If Name[A] > Name [B] swap Name[A] with Name [B] 3If B does not point to the last name on the list move B to the next name and go back to Step 2 4If there are 2 or more names after A move A to the next name move B to the name following A Go back to Step 2 Stop Name AAlice Jim Bill Nancy BBob
A Sort Algorithm Input: An unordered list of names Output:An ordered list of names Steps:1 Refer to the first two names as index A and B respectively 2If Name[A] > Name [B] swap Name[A] with Name [B] 3If B does not point to the last name on the list move B to the next name and go back to Step 2 4If there are 2 or more names after A move A to the next name move B to the name following A Go back to Step 2 Stop Name Alice AJim Bill Nancy BBob
A Sort Algorithm Input: An unordered list of names Output:An ordered list of names Steps:1 Refer to the first two names as index A and B respectively 2If Name[A] > Name [B] swap Name[A] with Name [B] 3If B does not point to the last name on the list move B to the next name and go back to Step 2 4If there are 2 or more names after A move A to the next name move B to the name following A Go back to Step 2 Stop Name Alice AJim BBill Nancy Bob
A Sort Algorithm Input: An unordered list of names Output:An ordered list of names Steps:1 Refer to the first two names as index A and B respectively 2If Name[A] > Name [B] swap Name[A] with Name [B] 3If B does not point to the last name on the list move B to the next name and go back to Step 2 4If there are 2 or more names after A move A to the next name move B to the name following A Go back to Step 2 Stop Name Alice AJim BBill Nancy Bob
A Sort Algorithm Input: An unordered list of names Output:An ordered list of names Steps:1 Refer to the first two names as index A and B respectively 2If Name[A] > Name [B] swap Name[A] with Name [B] 3If B does not point to the last name on the list move B to the next name and go back to Step 2 4If there are 2 or more names after A move A to the next name move B to the name following A Go back to Step 2 Stop Name Alice AJim BBill Nancy Bob
A Sort Algorithm Input: An unordered list of names Output:An ordered list of names Steps:1 Refer to the first two names as index A and B respectively 2If Name[A] > Name [B] swap Name[A] with Name [B] 3If B does not point to the last name on the list move B to the next name and go back to Step 2 4If there are 2 or more names after A move A to the next name move B to the name following A Go back to Step 2 Stop Name Alice ABill BJim Nancy Bob
A Sort Algorithm Input: An unordered list of names Output:An ordered list of names Steps:1 Refer to the first two names as index A and B respectively 2If Name[A] > Name [B] swap Name[A] with Name [B] 3If B does not point to the last name on the list move B to the next name and go back to Step 2 4If there are 2 or more names after A move A to the next name move B to the name following A Go back to Step 2 Stop Name Alice ABill BJim Nancy Bob
A Sort Algorithm Input: An unordered list of names Output:An ordered list of names Steps:1 Refer to the first two names as index A and B respectively 2If Name[A] > Name [B] swap Name[A] with Name [B] 3If B does not point to the last name on the list move B to the next name and go back to Step 2 4If there are 2 or more names after A move A to the next name move B to the name following A Go back to Step 2 Stop Name Alice ABill Jim BNancy Bob
A Sort Algorithm Input: An unordered list of names Output:An ordered list of names Steps:1 Refer to the first two names as index A and B respectively 2If Name[A] > Name [B] swap Name[A] with Name [B] 3If B does not point to the last name on the list move B to the next name and go back to Step 2 4If there are 2 or more names after A move A to the next name move B to the name following A Go back to Step 2 Stop Name Alice ABill Jim BNancy Bob
A Sort Algorithm Input: An unordered list of names Output:An ordered list of names Steps:1 Refer to the first two names as index A and B respectively 2If Name[A] > Name [B] swap Name[A] with Name [B] 3If B does not point to the last name on the list move B to the next name and go back to Step 2 4If there are 2 or more names after A move A to the next name move B to the name following A Go back to Step 2 Stop Name Alice ABill Jim BNancy Bob
A Sort Algorithm Input: An unordered list of names Output:An ordered list of names Steps:1 Refer to the first two names as index A and B respectively 2If Name[A] > Name [B] swap Name[A] with Name [B] 3If B does not point to the last name on the list move B to the next name and go back to Step 2 4If there are 2 or more names after A move A to the next name move B to the name following A Go back to Step 2 Stop Name Alice ABill Jim Nancy BBob
A Sort Algorithm Input: An unordered list of names Output:An ordered list of names Steps:1 Refer to the first two names as index A and B respectively 2If Name[A] > Name [B] swap Name[A] with Name [B] 3If B does not point to the last name on the list move B to the next name and go back to Step 2 4If there are 2 or more names after A move A to the next name move B to the name following A Go back to Step 2 Stop Name Alice ABill Jim Nancy BBob
A Sort Algorithm Input: An unordered list of names Output:An ordered list of names Steps:1 Refer to the first two names as index A and B respectively 2If Name[A] > Name [B] swap Name[A] with Name [B] 3If B does not point to the last name on the list move B to the next name and go back to Step 2 4If there are 2 or more names after A move A to the next name move B to the name following A Go back to Step 2 Stop Name Alice ABill Jim Nancy BBob
A Sort Algorithm Input: An unordered list of names Output:An ordered list of names Steps:1 Refer to the first two names as index A and B respectively 2If Name[A] > Name [B] swap Name[A] with Name [B] 3If B does not point to the last name on the list move B to the next name and go back to Step 2 4If there are 2 or more names after A move A to the next name move B to the name following A Go back to Step 2 Stop Name Alice ABill Jim Nancy BBob
A Sort Algorithm Input: An unordered list of names Output:An ordered list of names Steps:1 Refer to the first two names as index A and B respectively 2If Name[A] > Name [B] swap Name[A] with Name [B] 3If B does not point to the last name on the list move B to the next name and go back to Step 2 4If there are 2 or more names after A move A to the next name move B to the name following A Go back to Step 2 Stop Name Alice Bill AJim Nancy BBob
A Sort Algorithm Input: An unordered list of names Output:An ordered list of names Steps:1 Refer to the first two names as index A and B respectively 2If Name[A] > Name [B] swap Name[A] with Name [B] 3If B does not point to the last name on the list move B to the next name and go back to Step 2 4If there are 2 or more names after A move A to the next name move B to the name following A Go back to Step 2 Stop Name Alice Bill AJim BNancy Bob
A Sort Algorithm Input: An unordered list of names Output:An ordered list of names Steps:1 Refer to the first two names as index A and B respectively 2If Name[A] > Name [B] swap Name[A] with Name [B] 3If B does not point to the last name on the list move B to the next name and go back to Step 2 4If there are 2 or more names after A move A to the next name move B to the name following A Go back to Step 2 Stop Name Alice Bill AJim BNancy Bob
A Sort Algorithm Input: An unordered list of names Output:An ordered list of names Steps:1 Refer to the first two names as index A and B respectively 2If Name[A] > Name [B] swap Name[A] with Name [B] 3If B does not point to the last name on the list move B to the next name and go back to Step 2 4If there are 2 or more names after A move A to the next name move B to the name following A Go back to Step 2 Stop Name Alice Bill AJim BNancy Bob
A Sort Algorithm Input: An unordered list of names Output:An ordered list of names Steps:1 Refer to the first two names as index A and B respectively 2If Name[A] > Name [B] swap Name[A] with Name [B] 3If B does not point to the last name on the list move B to the next name and go back to Step 2 4If there are 2 or more names after A move A to the next name move B to the name following A Go back to Step 2 Stop Name Alice Bill AJim BNancy Bob
A Sort Algorithm Input: An unordered list of names Output:An ordered list of names Steps:1 Refer to the first two names as index A and B respectively 2If Name[A] > Name [B] swap Name[A] with Name [B] 3If B does not point to the last name on the list move B to the next name and go back to Step 2 4If there are 2 or more names after A move A to the next name move B to the name following A Go back to Step 2 Stop Name Alice Bill AJim BNancy Bob
A Sort Algorithm Input: An unordered list of names Output:An ordered list of names Steps:1 Refer to the first two names as index A and B respectively 2If Name[A] > Name [B] swap Name[A] with Name [B] 3If B does not point to the last name on the list move B to the next name and go back to Step 2 4If there are 2 or more names after A move A to the next name move B to the name following A Go back to Step 2 Stop Name Alice Bill AJim Nancy BBob
A Sort Algorithm Input: An unordered list of names Output:An ordered list of names Steps:1 Refer to the first two names as index A and B respectively 2If Name[A] > Name [B] swap Name[A] with Name [B] 3If B does not point to the last name on the list move B to the next name and go back to Step 2 4If there are 2 or more names after A move A to the next name move B to the name following A Go back to Step 2 Stop Name Alice Bill AJim Nancy BBob
A Sort Algorithm Input: An unordered list of names Output:An ordered list of names Steps:1 Refer to the first two names as index A and B respectively 2If Name[A] > Name [B] swap Name[A] with Name [B] 3If B does not point to the last name on the list move B to the next name and go back to Step 2 4If there are 2 or more names after A move A to the next name move B to the name following A Go back to Step 2 Stop Name Alice Bill ABob Nancy BJim
A Sort Algorithm Input: An unordered list of names Output:An ordered list of names Steps:1 Refer to the first two names as index A and B respectively 2If Name[A] > Name [B] swap Name[A] with Name [B] 3If B does not point to the last name on the list move B to the next name and go back to Step 2 4If there are 2 or more names after A move A to the next name move B to the name following A Go back to Step 2 Stop Name Alice Bill ABob Nancy BJim
A Sort Algorithm Input: An unordered list of names Output:An ordered list of names Steps:1 Refer to the first two names as index A and B respectively 2If Name[A] > Name [B] swap Name[A] with Name [B] 3If B does not point to the last name on the list move B to the next name and go back to Step 2 4If there are 2 or more names after A move A to the next name move B to the name following A Go back to Step 2 Stop Name Alice Bill ABob Nancy BJim
A Sort Algorithm Input: An unordered list of names Output:An ordered list of names Steps:1 Refer to the first two names as index A and B respectively 2If Name[A] > Name [B] swap Name[A] with Name [B] 3If B does not point to the last name on the list move B to the next name and go back to Step 2 4If there are 2 or more names after A move A to the next name move B to the name following A Go back to Step 2 Stop Name Alice Bill Bob ANancy BJim
A Sort Algorithm Input: An unordered list of names Output:An ordered list of names Steps:1 Refer to the first two names as index A and B respectively 2If Name[A] > Name [B] swap Name[A] with Name [B] 3If B does not point to the last name on the list move B to the next name and go back to Step 2 4If there are 2 or more names after A move A to the next name move B to the name following A Go back to Step 2 Stop Name Alice Bill Bob ANancy BJim
A Sort Algorithm Input: An unordered list of names Output:An ordered list of names Steps:1 Refer to the first two names as index A and B respectively 2If Name[A] > Name [B] swap Name[A] with Name [B] 3If B does not point to the last name on the list move B to the next name and go back to Step 2 4If there are 2 or more names after A move A to the next name move B to the name following A Go back to Step 2 Stop Name Alice Bill Bob ANancy BJim
A Sort Algorithm Input: An unordered list of names Output:An ordered list of names Steps:1 Refer to the first two names as index A and B respectively 2If Name[A] > Name [B] swap Name[A] with Name [B] 3If B does not point to the last name on the list move B to the next name and go back to Step 2 4If there are 2 or more names after A move A to the next name move B to the name following A Go back to Step 2 Stop Name Alice Bill Bob AJim BNancy
A Sort Algorithm Input: An unordered list of names Output:An ordered list of names Steps:1 Refer to the first two names as index A and B respectively 2If Name[A] > Name [B] swap Name[A] with Name [B] 3If B does not point to the last name on the list move B to the next name and go back to Step 2 4If there are 2 or more names after A move A to the next name move B to the name following A Go back to Step 2 Stop Name Alice Bill Bob AJim BNancy
A Sort Algorithm Input: An unordered list of names Output:An ordered list of names Steps:1 Refer to the first two names as index A and B respectively 2If Name[A] > Name [B] swap Name[A] with Name [B] 3If B does not point to the last name on the list move B to the next name and go back to Step 2 4If there are 2 or more names after A move A to the next name move B to the name following A Go back to Step 2 Stop Name Alice Bill Bob AJim BNancy
A Sort Algorithm Input: An unordered list of names Output:An ordered list of names Steps:1 Refer to the first two names as index A and B respectively 2If Name[A] > Name [B] swap Name[A] with Name [B] 3If B does not point to the last name on the list move B to the next name and go back to Step 2 4If there are 2 or more names after A move A to the next name move B to the name following A Go back to Step 2 Stop Name Alice Bill Bob AJim BNancy
Algorithm Analysis What is the Key Idea? - It makes use of an Exchange Sort - It compares 2 items and swaps them if they are out of order - This continues until we sweep through all of the names
Algorithm Analysis How efficient is this algorithm? - How many compares/swaps? - How many times do we “visit” each name on the list? - How can we characterize the “speed” or efficiency of this algorithm?
Algorithm Analysis How can we measure algorithm efficiency? - If there are n items in the list … - and we “visit” each item n times … - Then the algorithm is
Algorithm Analysis How can we measure algorithm efficiency? - If there are n items in the list … - and we “visit” each item n times … - Then the algorithm is ~n 2 complexity
Algorithm Analysis Can we do better than ~n 2 complexity? - Can we do ~n complexity? - Can we do ~2n complexity? - The most efficient algorithms are ~n log n