Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Algorithms
Week 2 Slides – Arrays and Algorithm
Analysis
What will we cover is this lecture?
• Object-oriented programming
– A revision of what you have already done in
CSE 1243 (Programming Paradigms)
• Arrays
– A simple Data Structure
• Introduction to Algorithm Analysis
– Empirical Analysis
– Asymptotic Analysis
OBJECT ORIENTED PROGRAMMING
A revision
Introduction to Object Oriented Programming [1]
Assuming that you have already covered object-oriented programming, this is just
a mere revision so that you can write more interesting programs with arrays….
• The idea of objects arose in the programming community
as a solution to the problems with procedural languages
whereby there was a poor correspondence between a
program and the real world.
– i.e. to improve abstraction
• An object contains both methods and variables.
– A thermostat object, for example, would contain not only
furnace_on() and furnace_off() methods, but also variables
called currentTemp and desiredTemp.
• In Java, an object’s variables such as these are called fields.
Introduction to Object Oriented Programming [2]
to 9. myArray[7]
– Note: If you use an index that’s less than 0 or myArray[8]
greater than the size of the array less 1, you’ll
get the Array Index Out of Bounds runtime error. myArray[9]
Array Initialization
• Unless you specify otherwise, an array of integers
is automatically initialized to 0 when it’s created.
• When we create an array of objects, e.g.
BankAccount[] B= new BankAccount[10];
– Until the array elements are given explicit values, they
contain the special null object.
– If you attempt to access an array element that
contains null, you’ll get the runtime error Null Pointer
Assignment.
– The idea is to make sure you assign something to an
element before attempting to access it.
An Array application [1]
class ArrayApp
{
public static void main(String[] args)
{
long[] arr; // reference to array
arr = new long[100]; // make array
int nElems = 0; // number of items
int j; // loop counter
long searchKey; // key of item to search for
//--------------------------------------------------------------
arr[0] = 77; // insert 10 items
arr[1] = 99;
arr[2] = 44;
arr[3] = 55;
arr[4] = 22;
arr[5] = 88;
arr[6] = 11;
arr[7] = 00;
arr[8] = 66;
arr[9] = 33;
nElems = 10; // now 10 items in array
An Array application [2]
//--------------------------------------------------------------
for(j=0; j<nElems; j++) // display items
System.out.print(arr[j] + “ “);
System.out.println(“”);
//--------------------------------------------------------------
searchKey = 66; // find item with key 66
for(j=0; j<nElems; j++) // for each element,
if(arr[j] == searchKey) // found item?
break; // yes, exit before end
if(j == nElems) // at the end?
System.out.println(“Can’t find “ + searchKey); // yes
else
System.out.println(“Found “ + searchKey); // no
An Array application [3]
//--------------------------------------------------------------
searchKey = 55; // delete item with key 55
for(j=0; j<nElems; j++) // look for it
if(arr[j] == searchKey)
break;
for(int k=j; k<nElems-1; k++) // move higher ones down
arr[k] = arr[k+1];
nElems--; // decrement size
//--------------------------------------------------------------
for(j=0; j<nElems; j++) // display items
System.out.print( arr[j] + “ “);
System.out.println(“”);
} // end main()
} // end class ArrayApp
10000
8000
6000
Time (ms)
4000
2000
0
1 2 3 4 5 6 7 8 9
Input size
Experimental Studies (cont.)
• Not as easy as it may at first appear…
– What is the question?
• Running time of average case?
• Which of two algorithms are faster?
• The values of parameters that optimize
performance?
• How close does an optimizing algorithm
come to an optimum value?
(Approximation algorithms)
Experimental Studies (cont.)
• Not as easy as it may at first appear…
– What do we measure?
• Actual running time of algorithm?
–What about context switches and the
like?
–Poor memory use (i.e. cache misses,
page faults) can give misleading results
–Platform dependence?
Experimental Studies (cont.)
Algorithm arrayMax(A, n)
Input array A of n integers
Output maximum element of A
currentMax ← A[0]
for i ← 1 to n − 1 do
if A[i] > currentMax then
currentMax ← A[i]
return currentMax
Pseudocode Details
• Control flow
– if … then … [else …]
– while … do …
– repeat … until …
– for … do …
– Indentation replaces braces
• Method declaration
Algorithm method (arg [, arg…])
Input …
Output …
Pseudocode Details (Cont.)
• Method call
var.method (arg [, arg…])
• Return value
return expression
• Expressions
←Assignment
(like = in C++/Java)
= Equality testing
(like == in C++/Java)
n2 Superscripts and other mathematical formatting allowed
The Random Access Machine (RAM) Model
• A CPU
• A potentially unbounded
bank of memory cells, each 2
1
of which can hold an 0
arbitrary number or
character
• Memory cells are numbered and accessing any cell in memory takes unit
time.
The RAM Model (Cont.)
• Each ``simple'' operation (+, *, -, =, if) takes exactly 1
time step.
• Loops and subroutines are not considered simple
operations.
– Instead, they are the composition of many single-step
operations.
– The time it takes to run through a loop or execute a
subprogram depends upon the number of loop iterations or
the specific nature of the function.
• Each memory access takes exactly one time step, and
we have as much memory as we need.
– The RAM model takes no notice of whether an item is in
cache or on the disk, which simplifies the analysis.
Primitive Operations
• Basic computations performed by an
algorithm
– Evaluating an expression
• Identifiable in pseudocode
– Assigning a value to a variable
• Largely independent from the
programming language
– Indexing into an array
Primitive Operations (Cont.)
• Exact definition not important (we will see
why later)
– Calling a method
• Assumed to take a constant amount of
time in the RAM model
– Returning from a method
Counting Primitive Operations
• By inspecting the pseudocode, we can
determine the maximum number of
primitive operations executed by an
algorithm, as a function of the input size
Counting Primitive Operations (Cont.)
1E+15
T
1E+13
1E+11
1E+9
1E+7
1E+5
1E+3
1E+1
1E-1
1E-1 1E+1 1E+3 1E+5 1E+7 1E+9
n
Constant Factors
1E+13
1E+11
1E+9
1E+7
1E+5
1E+3
1E+1
1E-1
1E-1 1E+1 1E+3 1E+5 1E+7 1E+9
n
Exercise 1