Lesson Objective: Understand what an algorithm is and be able to use them to solve a simple problem.

Slides:



Advertisements
Similar presentations
Garfield AP Computer Science
Advertisements

Equality Join R X R.A=S.B S : : Relation R M PagesN Pages Relation S Pr records per page Ps records per page.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Chapter 9: Searching, Sorting, and Algorithm Analysis
Dividing Polynomials.
Chapter 2 Polynomial and Rational Functions Copyright © 2014, 2010, 2007 Pearson Education, Inc Dividing Polynomials: Remainder and Factor Theorems.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Sorting.
Algorithm An algorithm is a step-by-step set of operations to be performed. Real-life example: a recipe Computer science example: determining the mode.
Ver. 1.0 Session 5 Data Structures and Algorithms Objectives In this session, you will learn to: Sort data by using quick sort Sort data by using merge.
CIS 101: Computer Programming and Problem Solving Lecture 6 Usman Roshan Department of Computer Science NJIT.
Discrete Mathematics Algorithms. Introduction Discrete maths has developed relatively recently. Its importance and application have arisen along with.
Algorithms. Problems, Algorithms, Programs Problem - a well defined task. –Sort a list of numbers. –Find a particular item in a list. –Find a winning.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 9 Searching.
Algorithms. Problems, Algorithms, Programs Problem - a well defined task. –Sort a list of numbers. –Find a particular item in a list. –Find a winning.
Chapter 3: The Fundamentals: Algorithms, the Integers, and Matrices
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
Searching and Sorting Gary Wong.
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Computer Science Searching & Sorting.
3.3: Dividing Polynomials: Remainder and Factor Theorems Long Division of Polynomials 1.Arrange the terms of both the dividend and the divisor in descending.
Adapted from instructor resource slides Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All.
Simple Iterative Sorting Sorting as a means to study data structures and algorithms Historical notes Swapping records Swapping pointers to records Description,
Adapted from instructor resource slides Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All.
CSC 211 Data Structures Lecture 13
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use by MSU Dept. of Computer Science.
Algorithms An algorithm is a set of instructions that enable you, step-by-step, to achieve a particular goal. Computers use algorithms to solve problems.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
To know and use the Bubble Sort and Shuttle Sort Algorithms.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n )
ALGORITHMS.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Recursion.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
Searching Topics Sequential Search Binary Search.
Young CS 331 D&A of Algo. Topic: Divide and Conquer1 Divide-and-Conquer General idea: Divide a problem into subprograms of the same kind; solve subprograms.
In the first pass, the first two numbers are compared. The shuttle sort compares the numbers in pairs from left to right exchanging when necessary.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Partial Fractions. After completing this chapter you should be able to: Add and subtract two or more fractions Convert an expression with linear factors.
DECISION 1. How do you do a Bubble Sort? Bubble Sort:  You compare adjacent items in a list;  If they are in order, leave them.  If they are not in.
INTRO2CS Tirgul 8 1. Searching and Sorting  Tips for debugging  Binary search  Sorting algorithms:  Bogo sort  Bubble sort  Quick sort and maybe.
1 Algorithms Searching and Sorting Algorithm Efficiency.
Sorting and Runtime Complexity CS255. Sorting Different ways to sort: –Bubble –Exchange –Insertion –Merge –Quick –more…
Sort Algorithm.
Searching and Sorting Algorithms
Bin Packing First fit algorithm
Discrete Mathematics Algorithms.
Warmup What is an abstract class?
Algorithm Analysis CSE 2011 Winter September 2018.
Teach A level Computing: Algorithms and Data Structures
Adapted from slides by Marty Stepp and Stuart Reges
Shuttle Sort Example 1st pass Comparisons: 1
Unit-2 Divide and Conquer
“Human Sorting” It’s a “Problem Solving” game:
Big O Notation.
Data Structures Review Session
Bin packing – First fit algorithm
Standard Version of Starting Out with C++, 4th Edition
Topic: Divide and Conquer
Decision Maths Unit 7 Sorting Algorithms 3. Shell Sort.
Bin Packing First fit algorithm
Shuttle Sort Example 1st pass Comparisons: 1
Core Assessments Core #1: This Friday (5/4) Core #2: Tuesday, 5/8.
“Human Sorting” It’s a “Problem Solving” game:
Presentation transcript:

Lesson Objective: Understand what an algorithm is and be able to use them to solve a simple problem

Monday’s child is fair of face Tuesday’s child is full of grace Wednesday’s child is full of woe Thursday’s child has far to go Friday’s child is loving and giving Saturday’s child works hard for its living And a child that is born on the Sabbath day Is fair and wise and good and gay If you don’t know what day of the week you were born on you can work it out using Zeller’s algorithm

Zeller’s algorithm Let day number = D Month number = M Year number = Y If M is 1 or 2 add 12 to M and subtract 1 from Y Let C be the first two digits of Y And Z be the last two digits of Y Add together the integer parts of (2.6M – 5.39), (Z÷4) and (C÷4), then add on D and Z and subtract 2C Find the remainder when this value is divided by 7. If the remainder is 0 the day was Sunday If the remainder is 1 the day was Monday etc. Eg 15 th May 1991 D =15 M = 5 Y = 1991 C = 19 Z = – 38 = So Wednesday

Russian peasant’s algorithm for long multiplication Step 1: Write down the two numbers to be multiplied Step 2: Beneath the left number write down double that number. Beneath the right number write down half of that number ignoring any remainder. Step 3: Repeat step 2 until the right number is 1 Step 4: Delete those rows where the number in the right column is even. Add up the remaining numbers in the left column. This is the answer you need

Euclid’s algorithm for finding the HCF of two integers Step 1: Pick two numbers Step 2: Find the difference between the two numbers to get a third number. Step 3: Keep the two smallest numbers of the three and cross out the third number Step 4: If the two numbers you are left with are the same then this number is the HCF of the original two numbers. If not then go back to Step 2.

Ways to communicate algorithms: 1)As a series of instructions expressed in words 2)As a Flow Chart 3)As a computer program – using ‘Pseudo Code’

Euclid’s algorithm for finding the HCF of two integers in words Step 1: Pick two numbers Step 2: Find the difference between the two numbers to get a third number. Step 3: Keep the two smallest numbers of the three and cross out the third number Step 4: If the two remaining numbers are equal then this number is the HCF of the original two numbers. If not go back to Step 2. Euclid’s algorithm for finding the HCF of two integers in a flow chart Read two numbers x,y x>y? Subtract y from x to get a new value for x x<y? x=y? Subtract x from y to get a new value for y Output x Euclid’s algorithm for finding the HCF of two integers in ‘Pseudo Code’ Input x Input y Repeat If x>y then x = x-y If y>x then y = y-x Until x=y Print x yes no yes no

Page 26 q.3, Page 27 q.5, Page 29 q 11, Page 41 q.19

Algorithmic Complexity There are many algorithms that can be used to solve some problems. The effectiveness of an algorithm can be judged in two ways: Its success rate and its efficiency Ways to judge success Some algorithms always find the best solution (these are the best kind of algorithms) Some algorithms find a solution, but not always the best (these are called Heuristic algorithms) Ways to judge efficiency The efficiency of an algorithm is a measure of its speed. This usually depends on the number of calculations required to complete the task A more efficient algorithm is one that completes the task using fewer calculations

Measuring the efficiency of an algorithm Consider calculating the value of a polynomial for some value of x For a linear: You will be required to do a*x + b 1 multiplications and 1 addition For a quadratic: You will be required to do a*x*x + b*x + c 3 multiplications and 2 additions For a cubic: You will be required to do a*x*x*x + b*x*x + c*x + d 6 multiplications and 3 additions For a quartic: You will be required to do a*x*x*x*x + b*x*x*x + c*x*x + d*x + e 10 multiplications and 4 additions In general you need to do 1 / 2 n(n+1) mults and n additions. In total: 1 / 2 n / 2 n calculations. We say the problem has quadratic complexity. The time to solve it is proportional to the size 2. If you double the ‘size’ of the problem it will take four times longer to solve. If you triple the ‘size’ it will take nine times longer. etc

Measuring the efficiency of an algorithm If you rewrite each polynomial in ‘nested’ form like this to begin with: (((ax + b)x + c)x + d)x + e Then for a linear a*x + b1 multiplication and 1 addition For a quadratic: (a*x + b)*x + c 2 multiplications and 2 additions For a cubic: ((a*x + b)*x + c)*x + d3 multiplications and 3 additions For a quartic: (((a*x + b)*x + c)*x + d)*x + e 4 multiplications and 4 additions In general you need n multiplications and n additions, a total of 2n calculations. The problem is said to have linear complexity and the time to solve is proportional to the size. Doubling the ‘size’ of the problem doubles the time to solve. Tripling the ‘size’ of the problem triples the time to solve etc.

Efficiency Problems 1)A sorting algorithm has quadratic efficiency. It takes 3 seconds to sort a list of 4000 items. How long will it take to sort a list of a) 8000 items? b) items? 2)An algorithm has cubic efficiency. It takes 10 seconds to solve a problem of ‘size’ 12. How long will it take to solve a problem of ‘size’ 30? 3)An algorithm takes 5 minutes to solve a problem of ‘size’ 30. How much longer would it take to solve a problem of size 40 if the algorithm has cubic efficiency rather than quadratic efficiency?

Bin Packing algorithms Suppose we have 11 vehicles to load onto a ferry with 5 lanes, each 15m long. The vehicles are labelled A to K and have different lengths as denoted. A 8m B 7m C 4m D 9m E 6m F 9m G 5m H 5m I 6m J 7m K 8m Can we fit all the vehicles onto the ferry?

Algorithms for solving this problem First Fit algorithm: Take the first item and place it in the first available ‘bin’ working from one end. Repeat until all the bins are full or until all items are packed. First Fit Decreasing algorithm: As above but sort the items into decreasing order of size first Full bins algorithm: Look for combinations of items that will make ‘full bins’ first and fill these bins, then use the First Fit algorithm to pack any remaining items

A B C D E F 4 Each block will be fitted into the first bin that has room for it. Bin packing – First fit algorithm 6 units

A B C D E F 4 The first block fits into the first bin Bin packing – First fit algorithm 6 units

A B C D E F 4 The second block fits in the first bin Bin packing – First fit algorithm 6 units

A B C D E F 4 The third block will not fit in the first bin Bin packing – First fit algorithm 6 units

A B C D E F But there is room in the second bin Bin packing – First fit algorithm 6 units

Bin packing – First fit algorithm The third bin is the first one the 5 will fit in A B C D E F units

The second bin has room for the 3 A B C D E F Bin packing – First fit algorithm 6 units

The fourth bin is the first one with room for the next one A B C D E F Bin packing – First fit algorithm 6 units

The next one also fits in the fourth bin A B C D E F Bin packing – First fit algorithm 6 units

Bin packing – First fit algorithm No room until the fifth bin for the 6 A B C D E F units

The 3 has to start a new bin A B C D E F Total usage is 6 bins. Bin packing – First fit algorithm 6 units

A B C D E F 4 With the first fit decreasing algorithm we sort the blocks into descending order first. Bin packing – First fit decreasing algorithm 6 units

With the first fit decreasing algorithm we sort the blocks into descending order first A B C D E F Bin packing – First fit decreasing algorithm 6 units

A B C D E F Now we use the first fit algorithm Bin packing – First fit decreasing algorithm 6 units

Bin packing – First fit decreasing algorithm A B C D E F Now we use the first fit algorithm units

A B C D E F Now we use the first fit algorithm Bin packing – First fit decreasing algorithm 6 units

A B C D E F Now we use the first fit algorithm Bin packing – First fit decreasing algorithm 6 units

A B C D E F Now we use the first fit algorithm Bin packing – First fit decreasing algorithm 6 units

A B C D E F Now we use the first fit algorithm Bin packing – First fit decreasing algorithm 6 units

1 6 2 A B C D E F Now we use the first fit algorithm Bin packing – First fit decreasing algorithm 6 units

1 6 A B C D E F Now we use the first fit algorithm Bin packing – First fit decreasing algorithm 6 units

6 A B C D E F Now we use the first fit algorithm We have packed them into 5 bins. 1 Bin packing – First fit decreasing algorithm 6 units

Sorting Algorithms

Algorithms for Sorting Items 1)Interchange sort algorithm 2)Bubble sort algorithm 3)Shuttle sort algorithm 4)Quick sort algorithm

The Interchange Sort Algorithm Step 1: Search down the list to find the smallest number and interchange it with the first number on the list Step 2: Starting with the 2 nd number on the list, search down the list to find the smallest number left and interchange it with the 2 nd number on the list. Step 3: Repeat Step 2 starting with the 3 rd, then 4 th number on the list until the bottom of the list is reached.

Original List After 1 Pass After 2 Passes After 3 Passes After 4 Passes After 5 Passes After 6 Passes

The Bubble Sort Algorithm Step 1: If there is only one number in the list then stop. Step 2: Make one pass down the list, comparing numbers in pairs and swapping them as necessary. Step 3: If no swaps have occurred then stop. Otherwise, ignore the last element of the list and return to Step 1.

Example Original List

Example Original List “Compare numbers in pairs”

Example Original List “swapping them as necessary”

Example Original List Making a pass down the list

Example Original List

Example Original List

Example Original List

Example Original List

Example Original List

Example Original List

Example Original List

Example Original List

Example Original List

Example Original List 1 st pass “ignore the last element”

Example Original List 1 st pass “return to Step 2”

Example Original List 1 st pass

Example Original List 1 st pass

Example Original List 1 st pass

Example Original List 1 st pass

Example Original List 1 st pass

Example Original List 1 st pass

Example Original List 1 st pass

Example Original List 1 st pass

Example Original List 1 st pass

Example Original List 1 st pass

Example Original List 1 st pass2 nd pass

Example Original List 1 st pass2 nd pass

Example Original List 1 st pass2 nd pass

Example Original List 1 st pass2 nd pass

Example Original List 1 st pass2 nd pass

Example Original List 1 st pass2 nd pass

Example Original List 1 st pass2 nd pass

Example Original List 1 st pass2 nd pass

Example Original List 1 st pass2 nd pass

Example Original List 1 st pass2 nd pass 3 rd pass

Example Original List 1 st pass2 nd pass3 rd pass

Example Original List 1 st pass2 nd pass3 rd pass

Example Original List 1 st pass2 nd pass3 rd pass

Example Original List 1 st pass2 nd pass3 rd pass

Example Original List 1 st pass2 nd pass3 rd pass

Example Original List 1 st pass2 nd pass3 rd pass

Example Original List 1 st pass2 nd pass3 rd pass4 th pass

Example Original List 1 st pass2 nd pass3 rd pass4 th pass

Example Original List 1 st pass2 nd pass3 rd pass4 th pass

Example Original List 1 st pass2 nd pass3 rd pass4 th pass

Example Original List 1 st pass2 nd pass3 rd pass4 th pass

Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass “If no swaps have occurred then stop.”

The Shuttle Sort Algorithm

1 st pass – Compare the 1 st and 2 nd numbers in the list and swap if necessary. 2 nd pass – Compare the 2 nd and 3 rd numbers in the list and swap if necessary. If a swap has occurred, compare the 1 st and 2 nd numbers and swap if necessary. 3 rd pass – Compare the 3 rd and 4 th numbers in the list and swap if necessary. If a swap has occurred, compare the 2 nd and 3 rd numbers, and so on up the list. And so on, through the entire list.

Example Original List

Example Original List 1 st pass “1 st pass”

Example Original List 1 st pass “compare the 1 st and 2 nd numbers”

Example Original List 1 st pass “swap if necessary”

Example Original List 1 st pass2 nd pass “2 nd pass”

Example Original List 1 st pass2 nd pass “compare the 2 nd and 3 rd numbers”

Example Original List 1 st pass2 nd pass “swap if necessary”

Example Original List 1 st pass2 nd pass “if a swap has occurred, compare the 1 st and 2 nd numbers”

Example Original List 1 st pass2 nd pass “swap if necessary”

Example Original List 1 st pass2 nd pass3 rd pass “3 rd pass”

Example Original List 1 st pass2 nd pass3 rd pass “compare 3 rd and 4 th numbers”

Example Original List 1 st pass2 nd pass3 rd pass “swap if necessary”

Example Original List 1 st pass2 nd pass3 rd pass “if a swap has occurred….”

Example Original List 1 st pass2 nd pass3 rd pass4 th pass

Example Original List 1 st pass2 nd pass3 rd pass4 th pass

Example Original List 1 st pass2 nd pass3 rd pass4 th pass

Example Original List 1 st pass2 nd pass3 rd pass4 th pass

Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass

Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass

Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass

Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass

Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass

Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass

Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass

Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass

Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass

Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass

Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass6 th pass

Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass6 th pass

Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass6 th pass

Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass6 th pass

Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass6 th pass

Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass6 th pass

Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass6 th pass

Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass6 th pass

Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass6 th pass

Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass6 th pass

Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass6 th pass

Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass6 th pass

Example Original List 1 st pass2 nd pass3 rd pass4 th pass5 th pass6 th pass

Example The example required 14 comparisons and 9 swaps to take place. The Bubble Sort algorithm requires 20 comparisons and 9 swaps for the same list. This makes the Shuttle Sort the more efficient algorithm.

The Quick Sort Algorithm Choose the first number in the list as a pivot and ring it. Pass through the list writing those numbers lower than or equal to the pivot before it in the list and those numbers larger than the pivot after it. This will split the list into two sub-lists with the pivot as a divide. Repeat the algorithm on each sub-list, choosing the first number in each sub-list as the pivot for that sub-list. Keep splitting the sub-lists in this way until no sub-list has more than one number in it

Example Original List

Example Ring the pivot Original List

Example Pass down the list, placing the numbers on the correct side of the pivot Original List First Pass

Example Ring the pivots in the sub-lists Original List First Pass

Example Original List First Pass Second Pass Pass down the list, placing the numbers on the correct side of each pivot

Example Original List First Pass Second Pass Ring the pivots in the new sub- lists

Example Original List First Pass Second Pass Pass down the list, placing the numbers on the correct side of each pivot Third Pass

Example Original List First Pass Second Pass Third Pass Ring the pivots in the sub-lists

Example Original List First Pass Second Pass Third Pass Fourth Pass Pass down the list, placing the numbers on the correct side of each pivot

Example Original List First Pass Second Pass Third Pass Fourth Pass No sub-lists of more than one so stop

Algorithms for ‘searching’ for an item on a list

The Binary Search Algorithm Use the Binary Search pps