ArrayBased Lists - PowerPoint PPT Presentation

1 / 51
About This Presentation
Title:

ArrayBased Lists

Description:

A list is a homogeneous collection of elements, with a linear relationship between the elements ... a negative number if the instance string is less than the ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 52
Provided by: sylvi152
Category:
Tags: arraybased | keys | lists

less

Transcript and Presenter's Notes

Title: ArrayBased Lists


1
  • Chapter 11
  • Array-Based Lists

2
Chapter 12 Topics
  • Insertion into and Deletion from an Unordered
    List
  • Straight Selection Sort
  • Insertion into and Deletion from a Sorted List
  • Abstract classes
  • Searching
  • Sequential
  • Binary
  • Complexity of Searching and Sorting
  • Using the Java Comparable Interface

3
What is a List?
  • A list is a homogeneous collection of elements,
    with a linear relationship between the elements
  • Linear means each list element (except the first)
    has a unique predecessor, and each element
    (except the last) has a unique successor

4
Basic Kinds of Class Operations
  • Constructor An operation that creates a new
    instance (object) of a class
  • Transformer An operation that changes the
    internal state of an object
  • Observer An operation that allows us to observe
    the state of an object without changing it

4
5
Additional Class Operations
  • Iterator An operation that allows us to process
    (one by one) all the components in an object)
  • Copy constructor An operation that creates a new
    instance of a class by copying an existing
    instance, (possibly altering some or all of its
    state in the process)

5
6
Class List Operations
  • Transformers
  • insert
  • delete
  • Observers
  • isEmpty
  • isFull
  • length
  • isThere
  • Iterators
  • resetList
  • getNextItem

change state observe state process
components
6
7
Class List Data Components
  • numItems
  • listItems0
  • . . .
  • listItemslistItems.length-1
  • currentPos

number of elements in list array of list
elements used by iterator
7
8

class List
List
Private data numItems listItems listItems
0 1
2 listItems.length-1 curren
tPos
List(int)
isEmpty
isFull
length
insert
delete
isThere
resetList
getNextItem
9
class List
  • // class List
  • public class List
  • // Data fields
  • protected String listItems
  • // Array to hold list items
  • protected int numItems
  • // Number of items currently in list
  • protected int currentPos
  • // State variable for iteration
  • . . .

9
10
Unsorted and Sorted Lists
UNSORTED LIST Elements are placed into the
list in no particular order with respect to
their content
SORTED LIST List elements are in an order
that is sorted by the content of their keys --
either numerically or alphabetically
11
Methods for Class List
  • public List() // Default Constructor
  • // Result List instantiated for 100 items
  • numItems 0
  • listItems new String100
  • currentPos 0
  • public List(int maxItems) // Constructor
  • // Result List instantiated for maxItems items
  • numItems 0
  • listItems new StringmaxItems
  • currentPos 0

12
Observer Methods
  • public boolean isEmpty()
  • // Returns true if no components false otherwise
  • return (numItems 0)
  • public int length()
  • // Returns the number of components in the list
  • return numItems

13
Observer Methods Contd.
  • public boolean isFull()
  • // Returns true if no more room false otherwise
  • return (numItems listItems.length)

14
Transformer Method Insert
  • public void insert(String item)
  • // Result If the list is not full, puts item in
  • // the last position in the list otherwise list
  • // is unchanged.
  • if (!isFull())
  • listItemsnumItems item
  • numItems

14
15
Before Inserting 64 into an Unsorted List
The item will be placed into the numItems
location, and numItems will be incremented.
numItems 3 listItems 0
15 1
39 2
-90 3 .
. .
listItems.length-1
15
16
After Inserting 64 into an Unsorted List
numItems 4 listItems 0
15 1
39 2
-90 3 64
. .
. listItems.length-1
16
17
String Method compareTo
  • When comparing objects with , the result is
    true only if both references refer to the same
    object in memory
  • String method compareTo uses a dictionary type
    comparison of strings and returns
  • 0 if they have the same letters in the same order
  • a negative number if the instance string is less
    than the string passed as a parameter
  • a positive number if the instance string is
    greater than the string that is passed

18
Values of each expression
String s1 new String(today) String s2
new String(yesterday) String s3 new
String(today) String s4 new
String(Today)
s1.compareTo(s2) -5 s1 s3 false
s1.compareTo(s3) 0 s1.compareTo(s4) 32
19
Observer Method isThere
  • public boolean isThere(String item)
  • // Returns true if item is in the list false
    otherwise
  • int index 0
  • while (index lt numItems
  • listItemsindex.compareTo(item) ! 0)
  • index
  • return (index lt numItems)

19
20
Transformer Method Delete
  • Find the position of the element to be deleted
    from the list
  • Eliminate the item being deleted by shifting up
    all the following list elements
  • Decrement numItems

21
Deleting 39 from a List
index 0
numItems 4 listItems 0
15 1
39 2 -90
3 64 . .
. listItems.length-1
found false
21
22
Deleting 39 Contd.
index 1
found false
22
23
Deleting 39 Contd.
index 1
found true
23
24
Deleting 39 Contd.
index 1
found true
24
25
Deleting 39 Contd.
index 1
found true
25
26
Deleting 39 Contd.
index 1
found true
26
27
  • public void delete(String item)
  • // Result Removes item from the list if it is
  • // there otherwise list is unchanged.
  • int index 0
  • boolean found false
  • while (index lt numItems !found)
  • if (listItemsindex.compareTo(item) 0)
  • found true
  • else
  • index

27
28
  • // If item found, shift remainder of list up
  • if (found)
  • for (int count index count lt numItems - 1
  • count)
  • listItemscount listItemscount 1
  • numItems--

29
Iterator Methods
  • public void resetList()
  • // Initialize iterator by setting currentPos to 0
  • currentPos 0
  • public String getNextItem()
  • // Returns current item increments currentPos
    circularly
  • // Assumption No transformers invoked since last
    call
  • String next listItemscurrentPos
  • if (currentPos numItems - 1)
  • currentPos 0
  • else
  • currentPos
  • return next

29
30
Straight Selection Sort
  • Examines the entire list to select the smallest
    element places that element where it belongs
    (with array index 0)
  • Examines the remaining list to select the
    smallest element from it places that element
    where it belongs (array index 1)
  • Continues process until only 2 items remain in
    unsorted portion
  • Examines the last 2 remaining list elements to
    select the smallest one place that element
    where it belongs in the array (index numItems-2,
    leaving the last item in its proper place as
    well.

31
Selection Sort Algorithm
  • FOR passCount going from 0 through numItems - 2
  • Find minimum value in listItems passCount .
    .
  • listItemsnumItems-1
  • Swap minimum value with listItems passCount

listItems 0 40
25 listItems 1 100

100 listItems 2 60 60 listItems
3 25
40 listItems 4 80 80
pass 0
32
Selection Sort Code
  • public void selectSort()
  • // Sorts array into ascending
  • String temp int passCount int sIndex
  • int minIndex // index of minimum so far
  • for(passCount 0 passCount lt numItems-1
    passCount)
  • minIndex passCount
  • // find index of smallest remaining
  • for(sIndex passCount 1 sIndex lt
    numItems sIndex)
  • if(listItemssIndex.compareTo(listItemsmi
    nIndex)lt0)
  • minIndex sIndex
  • temp listItemsminIndex // swap
  • listItemsminIndex listItemspassCount
  • listItemspassCount temp

33
Recall ...
UNSORTED LIST Elements are placed into the
list in no particular order with respect to
their content
SORTED LIST List elements are in an order
that is sorted by the content of their keys --
either numerically or alphabetically
34

Class SortedList
SortedList
Private data numItems listItems listItems
0 1
2 listItems.length-1 curren
tPos
SortedList(int)
isEmpty
isFull
length
insert
delete
isThere
resetList
getNextItem
35
Which Methods Change?
  • Which method must be changed to ensure that any
    instance of class SortedList remains sorted at
    all times?
  • insert
  • Which method can be changed to make it more
    efficient?
  • isThere

36
Insert Algorithm for SortedList
  • Create space for the new item by shifting down
    all the larger list elements
  • Put the new item in the list
  • Increment numItems

37
  • public void insert(String item)
  • // If the list is not full, puts item in its
    proper
  • // place otherwise list is unchanged.
  • // Assumption item is not already in the list.
  • if (! isFull())
  • // find proper location for new element
  • int index numItems - 1
  • while (index gt 0
  • item.compareTo(listItemsindex) lt 0)
  • listItemsindex 1 listItemsindex
  • index--
  • listItemsindex 1 item // insert item
  • numItems

38
List class hierarchy
List
ListWithSort
SortedList
39
Abstract List Class Hierarchy
List
SortedList
UnsortedList
ListWithSort
40
Improving method isThere
  • Recall that with the unsorted class List
  • we examined each list element beginning
  • with listItems0, until we either found a
  • match with item, or we had examined all
  • the elements in the unsorted List
  • How can the searching algorithm be improved for
    class SortedList?

41
Searching for 55
A sequential search for 55 can stop when 64 has
been examined
41
42
Binary Search in SortedList
  • Examines the element in the middle of the array.
    Is it the sought item? If so, stop searching.
    Is the middle element too small? Then start
    looking in second half of array. Is the middle
    element too large? Then begin looking in first
    half of the array
  • Repeat the process in the half of the array that
    should be examined next
  • Stop when item is found, or when there is nowhere
    else to look and item has not been found

43
  • public boolean isThere(String item)
  • // Assumption List items are in ascending order
  • // Returns true if item is in the list false
    otherwise
  • int first 0 int last numItems - 1
  • int middle boolean found false
  • while (last gt first !found)
  • middle (first last) / 2
  • if (item.compareTo(listItemsmiddle) 0)
  • found true // Item has been
    found
  • else if (item.compareTo(listItemsmiddle lt 0)
  • last middle - 1 // Look in first
    half
  • else first middle 1 // Look in second
    half
  • return found

44
Trace of Binary Search item 84

first
middle
last
item gt listItemsmiddle
first middle 1
15 26 38 57 62 78
84 91 108 119
0 1 2 3
4 5 6
7 8 9
first middle
last
item lt listItemsmiddle last middle
- 1
45
Trace Concludes item 84

first, last
middle
item gt listItemsmiddle first
middle 1
first,
last,
middle
item listItemsmiddle found true
46
Another Trace
item 45

first
middle
last
item lt listItemsmiddle
last middle - 1
15 26 38 57 62 78
84 91 108 119
0 1 2 3
4 5 6
7 8 9
first middle last
item gt listItemsmiddle first middle
1
47
Trace continued
item 45

item gt listItemsmiddle first middle
1
first,
middle,
last
item lt listItemsmiddle
last middle - 1
48
Trace Concludes item 45

last first


49
Comparison of Searches

Average Number of Iterations to Find
item Length Sequential Search
Binary Search
10 5.5 2.9
100 50.5 5.8
1,000 500.5 9.0
10,000 5000.5 12.4
50
Big-O Comparison of List Operations

OPERATION UnsortedList SortedList
isThere O(N) O(N) sequential
search O(log2N) binary search
insert O(1) O(N)
delete O(N) O(N)
selectSort O(N2)
51
Comparable Interface
  • Is part of the standard Java class library
  • Any class that implements the Comparable
    interface must implement method compareTo
  • String implements the Comparable interface
Write a Comment
User Comments (0)
About PowerShow.com