0% found this document useful (0 votes)
19 views21 pages

EELU DS Week2 L2

Uploaded by

khaledsokre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views21 pages

EELU DS Week2 L2

Uploaded by

khaledsokre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

By

Dr. Yasser Abdelhamid


y Abdelhamid

 ADT
 Lists
 Searching algorithms

Dr. Yasser Abdelhamid


y Abdelhamid

 http://geeksforgeeks.com/

Dr. Yasser Abdelhamid


y Abdelhamid

 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.

 ADT only mentions what operations are to be


performed but not how these operations will
be implemented.
 It does not specify how data will be organized
in memory and what algorithms will be used
for implementing the operations.
 It is called “abstract” because it gives an

Dr. Yasser Abdelhamid


implementation-independent view.
y Abdelhamid

 List is a data structure that stores


elements in an ordered and sequential
manner.
 “Ordered” in this definition means that
each element has a position in the list.
 It does not mean that the list elements are
sorted by value.
 A list can store repetitive elements which
means a single element can occur more
than once in a list.

Dr. Yasser Abdelhamid


y Abdelhamid

 List
defines a member of functions that
any list implementation inheriting from it
must support, along with their parameters
and return types.

Dr. Yasser Abdelhamid


y Abdelhamid

// 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();

// Move the current position one step left, no

Dr. Yasser Abdelhamid


change if already at beginning
public void prev();
y Abdelhamid

 There are two main representations of


lists.
 Array lists.
 Linked lists.

Array List

Dr. Yasser Abdelhamid


Linked List
y Abdelhamid

 Arraylists are a collection of similar data


items stored at contiguous memory
locations and elements can be accessed
randomly using indices of an array.

Dr. Yasser Abdelhamid


y Abdelhamid

 Random access of list elements using array


index.
 Simple declaration and manipulation.
 Traversal through the array is easy using a
single loop.
 Sorting is easy as it can be implemented by
less lines of code.

Dr. Yasser Abdelhamid


y Abdelhamid

 Listsize is fixed, and is defined at the time


of declaration.
 The whole size of the list is reserved
whether it is used or not.
 Insertion and deletion operations cost
more time as it needs multiple shift
operations.

Dr. Yasser Abdelhamid


y Abdelhamid

 Implement the List interface methods


using arrays in Java programming
language.

Dr. Yasser Abdelhamid


y Abdelhamid

 Given a list of records.


 Each record has an associated key.
 We are searching for a record containing
a particular key.
 Efficiency is quantified in terms of
average time analysis (number of
comparisons) to retrieve an item.

Dr. Yasser Abdelhamid


y Abdelhamid

 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”

Dr. Yasser Abdelhamid


y Abdelhamid

 Start at first element of the list.


 Compare the current value to the (key)
value which we are searching for.
 If the two values match, return current
position with success. Otherwise,
continue to the next element of the array.
 If the end of the list is reached, return -1
with failure.

Dr. Yasser Abdelhamid


y Abdelhamid

public static int linearSearch(int[] arr, int key){


for(int i=0;i<arr.length;i++){
if(arr[i] == key){
return i;
}
}
return -1;
}

Dr. Yasser Abdelhamid


y Abdelhamid

According to the definition of bog-O:


T(n) is O(g(n)) if there exist two constants c, n0
where T(n) <= c.g(n) for all values of n >= n0

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:

Dr. Yasser Abdelhamid


Draw the two functions T(n), c.g(n) indicating n0
y Abdelhamid

 Suppose that the first element in the array list


contains the variable key, then we have
performed one comparison to find the key.
 •Suppose that the second element in the array
list contains the variable key, then we have
performed two comparisons to find the key.
 Continue the analysis till the key is found in
the last element of the array list. In this case,
we have performed N comparisons (N is the
size of the array list) to find the key.
 Finally if the key is NOT in the array list, then
we would have performed N comparisons and
the key is NOT found and we would return -1.

Dr. Yasser Abdelhamid


y Abdelhamid

 Average number of comparisons is:


Found at first element Found at last element
(1 comparison) (n comparisons) Not found
(n comparisons)

1 + 2 + 3 + ⋯+ 𝑛 + 𝑛
𝑛+1

Number of trials

Therefore, the average case time complexity for

Dr. Yasser Abdelhamid


Linear or sequential search is O(N).

You might also like