Problem Solving and Algorithms

Slides:



Advertisements
Similar presentations
Problem Solving and Algorithms
Advertisements

Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
MATH 224 – Discrete Mathematics
Chapter 7 Problem Solving and Algorithms. 2 Chapter Goals Describe the computer problem-solving process and relate it to Polya’s How to Solve It list.
Problem Solving and Algorithms
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
8 Algorithms Foundations of Computer Science ã Cengage Learning.
HST 952 Computing for Biomedical Scientists Lecture 9.
High-Level Programming Languages
Cmpt-225 Sorting. Fundamental problem in computing science  putting a collection of items in order Often used as part of another algorithm  e.g. sort.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Arrays
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
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.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
Chapter 16: Searching, Sorting, and the vector Type.
Lecture 5 Searching and Sorting Richard Gesick. The focus Searching - examining the contents of the array to see if an element exists within the array.
Invitation to Computer Science, Java Version, Second Edition.
Reynolds 2006 Complexity1 Complexity Analysis Algorithm: –A sequence of computations that operates on some set of inputs and produces a result in a finite.
Chapters 7, 8, & 9 Quiz 3 Review 1. 2 Algorithms Algorithm A set of unambiguous instructions for solving a problem or subproblem in a finite amount of.
Chapter 12 Recursion, Complexity, and Searching and Sorting
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.
Chapter 19: Searching and Sorting Algorithms
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
LAB#7. Insertion sort In the outer for loop, out starts at 1 and moves right. It marks the leftmost unsorted data. In the inner while loop, in starts.
CPS120: Introduction to Computer Science Functions.
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.
Chapter 7 Problem Solving and Algorithms. 2 Problem Solving Problem solving The act of finding a solution to a perplexing, distressing, vexing, or unsettled.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
CSCI 51 Introduction to Programming March 12, 2009.
Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):
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.
Objectives At the end of the class, students are expected to be able to do the following: Understand the searching technique concept and the purpose of.
Chapter 8 High-Level Programming Languages. 2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
Algorithms Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin,
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
1 Principles of Computer Science I Honors Section Note Set 5 CSE 1341.
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.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
©Brooks/Cole, 2003 Chapter 8 Algorithms. ©Brooks/Cole, 2003 CONCEPTCONCEPT 8.1.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Chapter 16: Searching, Sorting, and the vector Type.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
1 compares each element of the array with the search key. works well for small arrays or for unsorted arrays works for any table slow can put more commonly.
Searching Arrays Linear search Binary search small arrays
Chapter 16: Searching, Sorting, and the vector Type
Chapter 9: Sorting and Searching Arrays
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
Problem Solving and Algorithms
Searching and Sorting Arrays
Standard Version of Starting Out with C++, 4th Edition
Search,Sort,Recursion.
Search,Sort,Recursion.
And now for something completely different . . .
The structure of programming
Thinking procedurally
Presentation transcript:

Problem Solving and Algorithms Chapter 7 Problem Solving and Algorithms

Chapter Goals The Computer Problem Solving Process Top-Down Design What is an Algorithm Common Programming Control Structures Selection Repetition Sub-programs

Chapter Goals Arrays of Data Unsorted arrays and sorted arrays Recursion and recursive algorithms

Chapter Goals Common algorithms and their hand-simulatiion In Particular… Bubblesort algorithm Quicksort algorithm Sequential Search algorithm Binary Search algorithm

Problem Solving

Computer Problem-Solving aka “Software Development Lifecycle” Analysis and Specification Phase Analyze Specification Algorithm Development Phase Develop algorithm Test algorithm Implementation Phase Code algorithm Maintenance Phase Use Maintain Can you name a recurring theme?

Phase Interactions Should we add another arrow? (What happens Problem Should we add another arrow? (What happens if the problem is revised?)

Solutions are in the form of Algorithms For computer science, our “answers” are algorithms

Algorithms Algorithm A set of unambiguous instructions for solving a problem Abstract Step An algorithmic step containing unspecified details Concrete Step An algorithm step in which all details are specified (a step that a computer will understand)

Developing an Algorithm Two methodologies are used to develop solutions Top-down design focuses on the tasks to be done Object-oriented design focuses on the data involved in the solution (More in Chapter 9)

Top-Down V Object Oriented Question: Which methodology is Best? Answer: Both are used Depends on the level of abstraction

Top-Down Methodology Start with Main Module (representing the entire problem) Break into a list of steps Each step becomes a sub-module Repeat for Each Sub-module Continue until all steps are CONCRETE Process ends when all steps are concrete

Top-Down Design Process continues until every step is concrete Name of (sub)problem at one level becomes a module at next lower level

Top-Down Wedding Planning

Algorithm Control Structures

Control Structures Control structure A construct that determines the order in which instructions are executed Mainly: Selection aka “Decisions” Iteration aka “Looping”

Selection Statements Flow of control of if statement

Algorithm with Selection Problem: Write the appropriate dress for a given temperature. Write "Enter temperature" Read temperature Determine Dress Which statements are concrete? Which statements are abstract? 18 18

Algorithm with Selection Determine Dress IF (temperature > 90) Write “Texas weather: wear shorts” ELSE IF (temperature > 70) Write “Ideal weather: short sleeves are fine” ELSE IF (temperature > 50) Write “A little chilly: wear a light jacket” ELSE IF (temperature > 32) Write “Philadelphia weather: wear a heavy coat” ELSE Write “Stay inside” 19 19

Looping Statements Flow of control of while statement

Looping in Psuedocode A count-controlled loop (Factorial of N) Set fact to 1 Set count to 1 Write "Please enter a number: " Read in N While (count <= N) Set fact to fact * count Increment count Write N + “Factorial is " + fact

Actual Code!! Now, with A count-controlled loop (Factorial of N) cout << "Please enter a number: "; cin >> N; while(count <= N){ fact = fact * count; count = count + 1; } cout << N << " Factorial is: " << fact;

Arrays

Arrays A set of homogeneous (same type) data elements requires a count of how many elements Data elements are accessed via an index: list[0] to list[count-1]

An Unsorted Array

Searching

Sequential Search Algorithm Look at every item until the item is found In More Detail… Look at the first item If it matches, we are done Look at the next item Repeat until the end of the list

Sequential Search Algorithm STRUCTURED Set Position to 0 Set found to FALSE WHILE (position < length AND NOT found ) IF (numbers [position] equals searchitem) Set Found to TRUE ELSE Set position to position + 1

Sorted Arrays A Sorting Algorithm puts array elements in either ascending or descending order. Pro: Sorting makes it easier to searches Con: Sorting takes time to do

A Sorted Array

Binary Search Algorithm A classic searching algorithm More efficient than sequential search But, data must be sorted already Binary search steps: Begin at the middle, eliminate half of the items, repeat on the remaining half

Binary Search - STRUCTURED Set first to 0 Set last to length-1 Set found to FALSE WHILE (first <= last AND NOT found) Set middle to (first + last)/ 2 IF (item equals data[middle])) Set found to TRUE ELSE IF (item < data[middle]) Set last to middle – 1 Set first to middle + 1 RETURN found

Binary Search lama Figure 7.10 Trace of the binary search

Binary Search Table 7.1 Average Number of Comparisons

Sorting

Sorting Sorting Arranging items in a collection so that they are in order Sorting algorithms Algorithms that put items in order

Bubble Sort Bubble Sort Algoritm: Compare a pair of items Put the smaller item on top Repeat the above for the next pair of items Repeat the entire process until the list is sorted

Bubble Sort

Bubble Sort - STRUCTURED Set firstUnsorted to 0 Set index to firstUnsorted + 1 Set swap to TRUE WHILE (index < length AND swap) Set swap to FALSE “Bubble up” the smallest item in unsorted part Set firstUnsorted to firstUnsorted + 1

Bubble Sort - STRUCTURED Bubble up Set index to length – 1 WHILE (index > firstUnsorted + 1) IF (data[index] < data[index – 1]) Swap data[index] and data[index – 1] Set swap to TRUE Set index to index - 1 40 40

Subprograms

Subprograms YAY Abstraction!! We can name a section of code and use it in another part of a program We can use this “block of code” as an abstraction YAY Abstraction!!

Subprogram Statements Figure 7.14 Subprogram flow of control

Subprograms in the Wedding “Ceremony” could be a written as a subprogram Abstraction helps us be organized subprogram

Subprograms and Data We can send data to/from a subprogram Arguments Data sent to a sub program Return Values Data returned from a subprogram

Subprogram Statements

Recursion

Recursive Stuff… Smaller versions repeat…

More Recursive Stuff…

Recursion Recursion The ability of a subprogram to call itself 1) Base case The case to which we have an answer 2) General case Expressing the solution as a smaller version of the problem

Recursion Factorial is the classic example. The factorial of a number is the number times the product of all the numbers between itself and 1. For example: 5! 5! = 5*4*3*2*1 = 120 Or, thinking recursively, 5! = 5*4!

Factorial via Recursion Base case Factorial(1) = 1 General Case Factorial(N) = N * Factorial(N-1)

Recursive Factorial Algorithm Write “Enter n” Read n Set result to Factorial(n) Write result + “ is the factorial of “ + n Factorial(n) IF (n equals 1) RETURN 1 ELSE RETURN n * Factorial(n-1) 53 53

Actual Code!! Now, with cout << "Please enter a number: "; cin >> n; result = Factorial(n); cout << n << " factorial is: " << result; int Factorial(int n) { if(n == 1) return 1; else return n * Factorial(n -1); } 54 54