EELU DS Week2 L2
EELU DS Week2 L2
ADT
Lists
Searching algorithms
http://geeksforgeeks.com/
Definition:
Abstract Data type (ADT) is a type (or class)
of objects whose state and behavior is
defined by a set of values and a set of
operations.
List
defines a member of functions that
any list implementation inheriting from it
must support, along with their parameters
and return types.
// List class ADT. Generalize by using "Object" for the // Move the current position one step right, no
element type. change if already at end
public interface List { // List class ADT public void next();
// Remove all contents from the list, so it is once again empty
public void clear(); // Return the number of elements in the list
public int length();
// Insert "it" at the current location
// The client must ensure that the list's capacity is // Return the position of the current element
not exceeded public int currPos();
public boolean insert(Object it);
// Set the current position to "pos“ (a specific
// Append "it" at the end of the list
position)
// The client must ensure that the list's capacity is not
public boolean moveToPos(int pos);
exceeded
public boolean append(Object it);
// Return true if current position is at end of the
list
// Remove and return the current element
public boolean isAtEnd();
public Object remove() throws
NoSuchElementException;
// Return the current element
public Object getValue() throws
// Set the current position to the start of the list
NoSuchElementException;
public void moveToStart();
public boolean isEmpty();
// Set the current position to the end of the list
}
public void moveToEnd();
Array List
Parameters
Array containing the list
Length of the list
Item that we are searching for
Result
If the item is found, report “success”, return
location in array
If the item is not found, report “not found” or
“failure”
T(n) = 3n+3
To prove that T(n) is O(n) we must find two
constants c, and n0 , where c >0 and n0 >= 0
and T(n) >= c.n for all values of n >= n0
let c = 4
we need to calculate the value of n0 that makes
the two lines T(n), 4n intersect
that is 3n+3 = 4n.
That makes n0 = 3
Assignment:
1 + 2 + 3 + ⋯+ 𝑛 + 𝑛
𝑛+1
Number of trials