Loading...

PPT – Simple algorithms on an array - compute sum and min PowerPoint presentation | free to download - id: 7bfb95-ZjU4M

The Adobe Flash plugin is needed to view this content

Simple algorithms on an array - compute sum and

min

Introduction

- In this webpage, we will apply the array

"traversal" for-statement

(a is some array (any type) ) int i

for ( i 0 i lt a.length i ) //

statements in the for-loop body will be //

executed ONCE for each array element ai

Introduction (cont.)

- to

- compute the sum of the elements in an array.
- find the smallest (minimum) value among the

elements in an array. - find the index of the array element that

contains the smallest (minimum) value among the

elements in an array.

Introduction (cont.)

- Recall that
- Make a note of this when we go through the

examples !

- A computer program (algorithm) must update some

information while it examines every possible

candidate solution, and - The information that the computer program must

maintain (update) is problem specific.

Note on the algorithms

- I will use the most basic for-statement in my

examples. - I will not take advantage of any specialized

syntax to keep my example as general (applicable

to other programming languages) as possible.

Computing the sum of the elements in an array

- Problem Description

- We are given a series of numbers stored inside

an array (say, array a)

Computing the sum of the elements in an array

(cont.)

- We must compute the sum of all the values store

in the array a - I.e compute 2.3 3.4 4.5 5.6 6.7

Computing the sum of the elements in an array

(cont.)

- Information that we must maintain

- sum the running sum of the array elements
- Before any array element has been added, the

running sum 0

Computing the sum of the elements in an array

(cont.)

- When we process the array element ai, we add

the value ai to the running sum - (The black arrow represents the array index

variable i)

Computing the sum of the elements in an array

(cont.)

- Algorithm is Pseudo code

sum 0.0 for all elements a0, a1,

a2, ... of array a do

add ai to sum print sum

Computing the sum of the elements in an array

(cont.)

- Java program

public class SumArray1 public

static void main(String args)

double a 2.3, 3.4 , 4.5, 5.6, 6.7, 7.8, 8.9

// 7 elements int i

// array index double sum // Running

sum for ( i 0 i lt a.length i )

sum sum ai // Add ai to

the running sum // Common error sum ai

// This will store ai into sum

System.out.println( sum )

Computing the sum of the elements in an array

(cont.)

- Example Program (Demo above code)
- Prog file
- http//www.mathcs.emory.edu/cheung/Courses/170/S

yllabus/09/Progs/SumArray1.java - How to run the program

- Right click on link and save in a scratch

directory - To compile javac SumArray1.java
- To run java SumArray1

Note on the example programs I use initialized

array

- I used an initialized array in my examples to

simplify the demo - In real life, you may need to read in the value

from the user. - It's easy to change the program by adding an

input segment before the summation algorithm.

Note on the example programs I use initialized

array (cont.)

import java.util.Scanner public class

SumArray2 public static void

main(String args) Scanner in new

Scanner(System.in) int n double a

System.out.print("How many numbers in input

") n in.nextInt() // n

values a new doublen // Create an

array of n elements int i /

------------------ Read in the values

------------------ /

- Example

Note on the example programs I use initialized

array (cont.)

- Example

for ( i 0 i lt a.length i )

System.out.print("Enter a number ") ai

in.nextDouble() // Read in number

/ ------------------ Compute the

sum ------------------ / double sum

sum 0.0 for ( i 0 i lt a.length i

) sum sum ai // Add ai to

the running sum // Common error sum ai

// This will store ai into sum

System.out.println( sum )

Note on the example programs I use initialized

array (cont.)

- The program section in red takes care of reading

in the input data into the array a

Note on the example programs I use initialized

array (cont.)

- Example Program (Demo above code)
- Prog file
- http//www.mathcs.emory.edu/cheung/Courses/170/S

yllabus/09/Progs/SumArray2.java - How to run the program

- Right click on link and save in a scratch

directory - To compile javac SumArray2.java
- To run java SumArray2

Find the minimum value in the elements of an

array

- Problem Description

- We are given a series of numbers stored inside

an array (say, array a) - We must find the minimum value of all the values

store in the array a - I.e find 1.2

Find the minimum value in the elements of an

array (cont.)

- Information that we must maintain

- min the current minimum value found among the

processed array elements

Find the minimum value in the elements of an

array (cont.)

- Initial attempt to construct the find minimum

algorithm

- Before any array element has been examined, set

min 0 - When we process the array element ai, we

change the minimum to ai IF

ai lt min

Find the minimum value in the elements of an

array (cont.)

- Examples

- Example 1
- In this example, the algorithm finds the correct

minimum value

Find the minimum value in the elements of an

array (cont.)

- Example 2
- Observation

- If the initial minimum value (0) is smaller than

all the values in the array, the algorithm will

find an incorrect minimum value

Find the minimum value in the elements of an

array (cont.)

- How to correct the problem
- Solution

- We need to use a correct initial value

- Use a0 as the initial minimum value
- Start comparing the array elements from a1

Find the minimum value in the elements of an

array (cont.)

- Example

Find the minimum value in the elements of an

array (cont.)

- Algorithm is Pseudo code

min a0 // Assume a0 is the minimum

for all elements a1, a2, ... of array a do

if ( ai lt min )

min ai // We found a smaller

minimum print min

Find the minimum value in the elements of an

array (cont.)

public class MinArray1 public

static void main(String args)

double a 2.3, 3.4 , 4.5, 5.6, 1.2, 7.8, 8.9

// 7 elements int i

// array index double min // Current

min value for ( i 1 i lt a.length

i ) if ( ai lt min )

min ai // Found a smaller min. value

System.out.println( min )

- Java program

Find the minimum value in the elements of an

array (cont.)

- Example Program (Demo above code)
- Prog file
- http//www.mathcs.emory.edu/cheung/Courses/170/S

yllabus/09/Progs/MinArray1.java - How to run the program

- Right click on link and save in a scratch

directory - To compile javac MinArray1.java
- To run java MinArray1

Find the index of the array element that contains

the minimum value

- Problem Description

- We are given a series of numbers stored inside

an array (say, array a)

Find the index of the array element that contains

the minimum value (cont.)

- We must find the index of the array element

containing the minimum value of all the values

store in the array a - I.e find 2

Find the index of the array element that contains

the minimum value (cont.)

- Information that we must maintain

- min_i the current index of the array element

that contains the minimum value found among the

processed array elements

Find the index of the array element that contains

the minimum value (cont.)

- We can modify the previous algorithm for finding

the minimum value to solve this problem. - Modified solution

- Use a0 as the initial minimum value by setting

min_i 0 - Start comparing the array elements from a1
- Update min_i when you find a smaller value

Find the index of the array element that contains

the minimum value (cont.)

- Example

Find the index of the array element that contains

the minimum value (cont.)

- Algorithm is Pseudo code

min_i 0 // Assume elem 0 (a0) is the

minimum for all element a1, a2, ... of

array a do if ( ai lt

amin_i ) min_i i // We

found a smaller minimum, update mn_i

print min_i

Find the index of the array element that contains

the minimum value (cont.)

- Java program

public class MinArray2 public

static void main(String args)

double a 2.3, 3.4 , 4.5, 5.6, 1.2, 7.8, 8.9

// 7 elements int i

// array index int min_i // Current

index with min value min_i 0

// Assume a0 is the current min.

value for ( i 1 i lt a.length i )

if ( ai lt amin_i )

min_i i // Found a smaller min. value, update

min_i System.out.println(

min_i )

Find the index of the array element that contains

the minimum value (cont.)

- Note

- The variable min_i is an integer
- (In the previous program, the variable min is a

double !)

Find the index of the array element that contains

the minimum value (cont.)

- Example Program (Demo above code)
- Prog file
- http//www.mathcs.emory.edu/cheung/Courses/170/S

yllabus/09/Progs/MinArray2.java - How to run the program

- Right click on link and save in a scratch

directory - To compile javac MinArray2.java
- To run java MinArray2

Efficiency considerations

- Real world computer programming

- When you write real world computer applications

that process a large amount of data, you need to

pay some attention to the efficiency of the

computer program.

Efficiency considerations (cont.)

- Fact on computers in 2011

- One of the bottle necks ( the constraining

component) of computers in 2011 is accessing RAM

memory - The CPU can execute instructions much faster

than the memory can fetch them

- You want to write computer programs that uses

less access operations to the RAM memory

Efficiency considerations (cont.)

- One of the areas where you can reduce RAM memory

access operations is in array operations Facts

- To access ( read or update) a simple variable

(such as min), the computer needs to access the

RAM memory once - To access ( read or update) an array element

(such as ai), the computer needs to access the

RAM memory twice

- The computer first need to access ( read) the

variable i from RAM memory (Only after the

computer obtained the value of the uses the

variable i, it can find out the location of the

variable ai) - Then the computer need to access ( read or

write) the variable ai

Efficiency considerations (cont.)

- Consider the previous Java program

public class MinArray2 public

static void main(String args)

double a 2.3, 3.4 , 4.5, 5.6, 1.2, 7.8, 8.9

// 7 elements int i

// array index int min_i // Current

index with min value min_i 0

// Assume a0 is the current min. value

for ( i 1 i lt a.length i )

if ( ai lt amin_i ) // Inefficient

min_i i // Found a

smaller min. value, update min_i

System.out.println( min_i )

Efficiency considerations (cont.)

- We can improve the efficiency by use a simple

variable (min) to store amin_i and use that

variable in the comparison

public class MinArray3 public

static void main(String args)

double a 2.3, 3.4 , 4.5, 5.6, 1.2, 7.8, 8.9

// 7 elements int i

// array index int min_i // Current

index with min value double min //

min amin_i for efficiency min_i

0 // Assume a0 is the current min.

value min a0 // For efficiency

Efficiency considerations (cont.)

for ( i 1 i lt a.length i )

if ( ai lt min ) min_i i

// Found a smaller min. value, update min_i

min ai // For efficiency

System.out.println( min_i )

Efficiency considerations (cont.)

- Note

- We must make certain that the variable min

contains the value amin_i all the time. How to

achieve this

- Whenever the variable min_i is updated, we must

also update the variable min_i !

Efficiency considerations (cont.)

- Example Program (Demo above code)
- Prog file
- http//www.mathcs.emory.edu/cheung/Courses/170/S

yllabus/09/Progs/MinArray3.java - How to run the program

- Right click on link and save in a scratch

directory - To compile javac MinArray3.java
- To run java MinArray3