33
Chapters 7, 8, & 9 Quiz 3 Review 1

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

Embed Size (px)

Citation preview

Page 1: 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

Chapters 7, 8, & 9

Quiz 3 Review

1

Page 2: 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

2

Algorithms

Algorithm

A set of unambiguous instructions for solving a problem or subproblem in a finite amount of time using a finite amount of data

Abstract Step

An algorithmic step containing unspecified details

Concrete Step

An algorithm step in which all details are specified

Page 3: 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

3

Developing an Algorithm

Two methodologies used to develop computer solutions to a problem

– Top-down design focuses on the tasks to be done

– Object-oriented design focuses on the data involved in the solution (We will discuss this design in Ch. 9)

Page 4: 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

4

Summary of Methodology

Analyze the ProblemUnderstand the problem!!Develop a plan of attack

List the Main Tasks (becomes Main Module)Restate problem as a list of tasks (modules)Give each task a name

Write the Remaining ModulesRestate each abstract module as a list of tasksGive each task a name

Re-sequence and Revise as NecessaryProcess ends when all steps (modules) are concrete

Page 5: 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

5

Control Structures

Control structure

An instruction that determines the order in which other instructions in a program are executed

Page 6: 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

6

Selection Statements

Flow of control of if statement

Page 7: 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

7

Looping Statements

Flow of control of while statement

Page 8: 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

Arrays

As data is being read into an array, a counter is updated so that we always know how many data items were stored

If the array is called list, we are working with

list[0] to list[length-1] or

list[0]..list[length-1]

8

Page 9: 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

9

Sorted Arrays

The values stored in an array have unique keys of a type for which the relational operators are defined

Sorting rearranges the elements into either ascending or descending order within the array

A sorted array is one in which the elements are in order

Page 10: 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

10

Binary Search

Sequential search

Search begins at the beginning of the list and continues until the item is found or the entire list has been searched

Binary search (list must be sorted)

Search begins at the middle and finds the item or eliminates half of the unexamined items; process is repeated on the half where the item might be

Say that again…

Page 11: 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

11

Subprogram Statements

Page 12: 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

12

Recursion

Recursion

The ability of a subprogram to call itself

Base case

The case to which we have an answer

General case

The case that expresses the solution in terms of a call to itself with a smaller version of the problem

Page 13: 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

13

Recursion

For example, the factorial of a number is defined as the number times the product of all the numbers between itself and 0:

N! = N * (N 1)!

Base case

Factorial(0) = 1 (0! is 1)

General Case

Factorial(N) = N * Factorial(N-1)

Page 14: 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

14

CHAPTER 8

Page 15: 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

15

Logical Implementations

Two logical implementations of containers:

Array-based implementation

Objects in the container are kept in an array

Linked-based implementation

Objects in the container are not kept physically together, but each item tells you where to go to get the next one in the structure

Did you ever play treasure hunt, a game in which each clue

told you where to go to get the next clue?

Page 16: 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

16

Stacks

Stack

An abstract data type in which accesses are made at only one end

– LIFO, which stands for Last In First Out

– The insert is called Push and the delete is called Pop

Name three everydaystructures that are stacks

Page 17: 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

17

Queues

Queue

An abstract data type in which items are entered at one end and removed from the other end

– FIFO, for First In First Out

– No standard queue terminology• Enqueue, Enque, Enq, Enter, and Insert

are used for the insertion operation• Dequeue, Deque, Deq, Delete, and Remove

are used for the deletion operation.

Name three

everydaystructuresthat arequeues

Page 18: 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

18

Stacks and Queues

Stack and queue visualized as linked structures

Page 19: 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

19

Linked Implementations

Page 20: 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

20

Trees

Binary tree

A linked container with a unique starting node called the root, in which each node is capable of having two child nodes, and in which a unique path (series of nodes) exists from the root to every other node

A picture is worth athousands words…

Page 21: 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

21

Binary Search Trees

Binary search tree (BST)

A binary tree (shape property) that has the (semantic) property that characterizes the values in a node of a tree:

The value in any node is greater than the value in any node in its left subtree and less than the value in any node in its right subtree

Page 22: 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

22

Binary Search Tree

Figure 8.7 A binary search tree

Each nodeis the root

of a subtreemade up ofits left and

right children

Prove that thistree is a BST

Page 23: 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

23

Graphs

Graph

A data structure that consists of a set of nodes (called vertices) and a set of edges that relate the nodes to each other

Undirected graph

A graph in which the edges have no direction

Directed graph (Digraph)

A graph in which each edge is directed from one vertex to another (or the same) vertex

Page 24: 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

24

Graphs

Figure 8.10Examples of graphs

Page 25: 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

25

CHAPTER 9

Page 26: 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

26

Object-Oriented Design

Object-oriented Design

A problem-solving methodology that produces a solution to a problem in terms of self-contained entities called objects

Object

A thing or entity that makes sense within the context of the problem

For example, a student, a car, time, date

Page 27: 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

Translation Process

A program written in a high-level language must be translated into machine code

The machine code is then executed

Compilers and Interpreters are software tools employed to help with the translation process

27

Page 28: 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

28

Compilers

High-level language

A language that provides a richer (more English-like) set of instructions

Compiler

A program that translates a high-level language program into machine code

Page 29: 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

29

Compilers

Figure 9.2 Compilation process

How does this differ fromthe assembly process?

Page 30: 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

30

Interpreters

Interpreter

A translating program that translates and executes the statements in sequence

– Assembler or compiler produce machine code as output, which is then executed in a separate step

– An interpreter translates a statement and then immediately executes the statement

– Interpreters can be viewed as simulators

Page 31: 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

31

Java

•Introduced in 1996 and became instantly popular

•Portability was of primary importance

•Java is compiled into a standard machine language called Bytecode

•A software interpreter called the JVM (Java Virtual Machine) takes the Bytecode program and executes it

Page 32: 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

32

Portability

PortabilityThe ability of a program to be run on different machines

Compiler portabilityA program in a standardized language can be compiled and run on any machine that has the appropriate compiler

Bytecode portabilityA program translated into Bytecode can be run on any machine that has a JVM

Do you understand the difference?

Page 33: 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

33

Asynchronous Processing

Asynchronous processing

Not synchronized with the program's action– Clicking has become a major form of input

to the computer

– Mouse clicking is not within the sequence of the program

– A user can click a mouse at any time during the execution of a program