ALGO
ALGO
ALGO
The technical terms normally used for these three aspects are:
1. Specification
2. Verification
3. Performance analysis
Fundamental questions about
algorithms
The specification should formalize the crucial details of the
problem that the algorithm is intended to solve.
Verification involves making sure an algorithm satisfies its
specification
For simple problems, it is often easy to see that a particular
algorithm will always work
However, for more complicated specifications and/or algorithms,
the fact that an algorithm satisfies its specification may not be
obvious at all.
In this case, we need to spend some effort verifying whether the algorithm
is indeed correct.
In general, testing on a few particular inputs can be enough to show that
the algorithm is incorrect. However, since the number of different potential
inputs for most algorithms is infinite in theory, and huge in practice, more
than just testing on particular cases is needed to be sure that the algorithm
satisfies its specification.
We need correctness proofs.
Fundamental questions about
algorithms
Finally, the efficiency or performance of an algorithm relates
to the resources required by it, such as how quickly it will run,
or how much computer memory it will use.
…
1st 2nd … nth
component
Arrays in Java
arrays are Java objects
has attributes & methods attached
created by the new operator
but NOT via a constructor
i.e. NO “()”
instead, you have a pair of square
brackets
i.e. “[]”
When to Use Arrays?
when all items are homogeneous
i.e. of the same type
need fast access to individual item
because components are stored in a
continuous piece of memory
the maximum size is known
items need to be ordered
How to Use Arrays?
involve 3 steps:
declare array
i.e. attribute/ variable declaration
create/construct array object
i.e. calling “new” with an array size
access array component(s)
i.e. read/ write
Step #1 - Array Declaration
attribute/
int[] daysInMonth; variable
declaration,
String month[]; no size
specified
char myCharArray[];
“[]” go after data type/ variable name
myCharArray=new char[100];
daysInMonth=new int[12];
month=new String[12];
Note
you use “[]” instead of “()”
Shortcut to Array Creation &
Initialisation
declaration, creation & assignment in
one line
int[] prime={2,3,5,7,11,13};
index value
prime
0 1 2 3 4 5
2 3 5 7 11 13
1st 2nd 3rd 4th 5th 6th
element
Step #3 - Accessing Array
Elements
use the “[]” to specify the index
index value ranges from 0 to "length-1"
get value of an array component
month[10]
assign value to an array component
month[0]="January";
a "for" loop is usually used to process
array components
Graphical Representation of an
Array of String
"February" 2nd
String[] month;
month=new String[12];
month[0]=“January”;
month[1]=“February”;
…
"December" 12th
Array Size
each array has a "length" attribute
"length" is an attribute, not a method!
so NO brackets “()”
the following code scans the whole
array and prints out each component:
for (int i=0;i<month.length;i++)
System.out.println(month[i]);
Copying Arrays
array variables hold object references
assigning 1 array variable to another
make them point to the same array
object
NOT copying!
p1 2 3 5 7
int[] p1={2,3,5,7};
p2
int[] p2=new int[4];
p2=p1;
How to Copy an Array Properly?
you can copy the components 1 by 1
use a "for" loop
int[] p1={2,3,5,7};
int[] p2=new int[p1.length];
for (int i=0;i<p1.length;i++)
p2[i]=p1[i]; p1 2 3 5 7
p2 2 3 5 7
How to Copy an Array Properly?
(approach #2)
Array inherits the "clone()" method
from the Object class
int[] p1={2,3,5,7};
int[] p2=(int[])p1.clone();
"clone()" returns an Object, need to
cast it to a "int[]" type
Limitations of Arrays
you cannot expand an array
size is fixed on creation
order of components is fixed
you may shuffle them
2D Arrays
distance between 2 cities
Sokoto B/Kebbi Gusau Kaduna
interfaces
java.util.List define a set of methods
that the implementing
java.util.Set classes MUST defined/
implement
java.util.Map
The java.util.List
Interface
duplicates allowed
ordered
no maximum limit
some concrete classes implementing
the java.util.List interface:
java.util.Vector
java.util.ArrayList
java.util.LinkedList
java.util.List (cont'd)
each node stores an Object
implement a linked-list
can add to beginning/end of list
...
java.util.List Example
import java.util.*;
public class Test
{
public static void main(String argv[])
{
List x=new LinkedList();
x.addFirst("hello");
x.addLast("world");
} //end method main
} //end class Test
The java.util.Set
Interface
no duplicates (because it is a set!)
un-ordered
you can
add/remove element
check membership/existence of an
element
find the size of set (i.e. no. of
elements)
java.util.Set (cont'd)
2 implementations: HashSet,
TreeSet
Set x=new TreeSet();
x.add("Hello");
x.add("World");
System.out.println(x.contains("Hello"));
x.remove("Hello");
System.out.println(x.contains("Hello"));
The java.util.Map
Interface
key-value pairs
e.g. a phone directory
each key must be unique
2 implementations: HashMap, TreeMap
main operations:
add/remove a key-value pair
look up using key
get all keys
java.util.Map Example
Map directory=new HashMap();
directory.put(“Bello","262626");
directory.put(“Bala","264321");
String
phone=(String)directory.get(“
Bello");
The java.util.Iterator
Interface
allow iteration through all elements in a
list/set
like a "for" loop in an array
the iterator() methods returns an
Iterator object
use hasNext() to test for existence of
next element
use next() to get next element (if exists)
Iterator in a List
Collection c=new LinkedList();
... //add some elements into c
Iterator i=c.iterator();
Object x;
while (i.hasNext())
{
x=i.next(); //get next element
... //do something
}
Iterator in a Map
Set keyset=m.keySet();
Iterator i=keyset.iterator();
while (i.hasNext())
{
Object key=i.next();//get next key
Object value=m.get(key);//get
value
}
Summary
collection of objects can be implemented by
arrays/ abstract data types
(collection/container)
some container/collection abstract data types
are defined in the java.util package
handy & easy to use
no need to know their implementation details
you can also define your own abstract data
types
Readings
Deitel & Deitel Chapters 7 (Arrays)