1. Trang chủ
  2. » Công Nghệ Thông Tin

Lecture Java methods: Object-oriented programming and data structures (2nd AP edition): Chapter 4 - Maria Litvin, Gary Litvin

31 18 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 31
Dung lượng 427,25 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Chapter 4 - Algorithms. This chapter’s objectives are to: Understand general properties of algorithms, get familiar with pseudocode and flowcharts, learn about iterations and recursion, learn about working with lists, learn basic facts about OOP.

Trang 1

and Data Structures

Maria Litvin ● Gary Litvin

2nd AP edition with GridWorld

Trang 2

Objectives:

Trang 3

Define Algorithm

abstract step-by-step recipe that describes

how to perform a certain task or solve a

Trang 5

Repeat the following

three steps while k n:

Trang 7

pos = pos0 and dir = dir0?

Yes No

Yes

No

pos pos0 dir dir0

Step forward

Input:

pos0, dir0

Stop Wall in front?

Trang 8

Variables

be written and later erased and replaced with another value

sum  sum + sq

sum

Trang 9

Properties of Algorithms

or recursion to repeat the same steps multiple times

“size” of task or any input values

on a particular computer language or platform (although it may depend on the general

computing model)

Trang 10

Repeat the following

three steps while k n:

the algorithm repeats the same instructions many times, but with different values of the variables

(The “running time” depends on n,

of course)

General: works for

any n

Trang 11

Python C/C++

Javapublic class MyMath{

public static int addSquares(int n) {

int sum = 0;

for (int k = 1; k <= n; k++) sum += k * k;

return sum;

}}

int addSquares(int n){

int k, sum = 0;

for (k = 1; k <= n; k++) sum += k * k;

return sum;

}

Trang 12

Iterations

instructions multiple times

Trang 13

while (k <= n) {

sum += k * k; // add k * k to sum k++; // increment k by 1 }

Trang 14

for (<initial setup>; <as long as this condition holds>;

<adjust variable(s) at the end of each iteration>) {

Trang 15

Recursion

for a particular task in terms of applying the same procedure to a similar but smaller task.

simple that no recursion is needed.

a base case.

Trang 16

Recursion: an Example

Procedure: Climb steps

Base case: if no steps

to climb stop Recursive case: more steps to climb

1 Step up one step

2 Climb steps

Trang 17

Recursive Methods

Trang 18

Recursion: How Does it Work

iterations, but hidden from the programmer

addSquares (4)addSquares (3)

addSquares (2)

addSquares (1) addSquares (0)

3

2

0 1

0 1

5

14

Base case

Trang 19

Recursion (cont’d)

Recursion is especially useful for dealing with nested structures or branching processes

Trang 20

count count + the number of bytes in X

else (if X is a folder)

count   count + totalBytes(X)

}

return count

}

Base case(This is pseudocode, not Java!)

Trang 21

Euclid’s Algorithm

two positive integers

Trang 22

No

Trang 23

Euclid’s Algorithm (cont’d)

• With iterations • With recursion

public static int gcf (int a, int b)

Trang 24

Working with Lists

numbered

quickly

Amy 5

Ben 3

Cal 2

Dan

0

Eve 6

In Java, the elements

are counted from 0

Fay 1

Guy 4

Trang 25

List Traversal

 Start at the first element

 While more elements remain

process the next element

for (int i = 0; i < list.length; i++)

Java’s “for each” loop

(a.k.a enhanced for

loop)

Trang 26

Sequential Search

n, where n is the length of the list

Amy 5

Ben 3

Cal 2

Dan

0

Eve 6

Fay 1

Guy 4

Trang 27

Binary Search

in ascending (or descending) order

the middle element of the remaining

search range

Trang 28

Binary Search (cont’d)

Fay 5

Dan 3

Cal 2

Amy

0

Guy 6

Ben 1

Eve 4

Eve?

Fay 5

Dan 3

Cal 2

Amy

0

Guy 6

Ben 1

Eve 4

Fay 5

Dan 3

Cal 2

Amy

0

Guy 6

Ben 1

Eve 4 Eve?

Eve!

Trang 29

• For a list of 1,000,000 elements takes, on average, only 20 comparisons

Trang 30

Review:

• Why algorithms often use iterations?

• How is pseudocode different from Java code?

• Name three basic shapes used in flowcharts.

• Explain how variables are used in iterations.

• Which Java statements can be used to express iterations?

Trang 31

Review (cont’d):

• What is called a base case in recursion?

• Suppose we define “word” as a sequence of letters Turn this into a recursive definition.

• When does Binary Search apply?

• How many comparisons, on average, are needed to find one of the values in a list of 1000 elements using Sequential Search? Binary Search?

Ngày đăng: 04/11/2020, 23:14

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN