You are on page 1of 18

Sorted Lists

Chapter 13
Chapter Contents
Specifications for the ADT Sorted List
• Using the ADT Sorted List

A Linked Implementation
• The Method add
• The Efficiency of the Linked Implementation

An Implementation that Uses the ADT List


• Efficiency Issues

2
Specifications for the ADT Sorted List
Data
• A collection of objects in sorted order, same data type
• The number of objects in the collection
Operations
• Add a new entry Note:
Note:aasorted
sortedlist
listwill
willnot
not
let
letyou
youadd
addororreplace
replacean an
• Remove an entry entry
entryby
byposition
position
• Get the position of the entry
• Check if a certain value is contained in the list
• Clear the list
• Return the length of the list
• Check if list is empty or full
• Display the list 3
A Linked Implementation
Outline of the class
public class SortedLinkedList implements SortedListInterface
{ private Node firstNode; // reference to first node of chain
private int length; // number of entries in sorted list
public SortedLinkedList()
{ firstNode = null;
length = 0;
} // end default constructor
< Implementations of the sorted list operations go here. >
...
private class Node
{
private Object data;
private Node next;
< Constructors >
...
< Accessor and mutator methods: getData, setData, getNextNode, setNextNode
...
} // end Node
} // end SortedLinkedList 4
The Method add

Fig. 13-1 Insertion points of names into a


sorted chain of linked nodes.

5
The Method add
Recursive algorithm

if ( (currentNode = = null) or
newEntry.compareTo(currentNode.getData()) <= 0)
{
currentNode = new Node(newEntry, currentNode)
}
else
Recursively add newEntry to the chain beginning at
currentNode.getNextNode()

6
The Method add

Fig. 13-2 Recursively adding Luke to a


7
sorted chain of names
The Method add

Fig. 13-3 Recursively adding a node at


the beginning of the chain … continued →
8
The Method add

Fig. 13-3 (ctd) Recursively adding a node


9
at the beginning of the chain.
The Method add

Fig. 13-4 Recursively adding a node between


10
existing nodes in a chain … continued →
The Method add

Fig. 13-4 (ctd) Recursively adding a node


11
between existing nodes in a chain.
Efficiency of the Linked Implementation

ADT Sorted List Operation Array Linked


add(newEntry) O(n) O(n)
remove(anEntry) O(n) O(n)
getPosition(anEntry) O(n) O(n)
getEntry(givenPosition) O(1) O(n)
contains(anEntry) O(n) O(n)
remove(givenPosition) O(n) O(n)
display() O(n) O(n)
clear(), getLength(), O(1) O(1)
isEmpty(), isFull()

Fig. 13-5 The worst-case efficiencies of the operations


on the ADT sorted list for two implementations
12
An Implementation That Uses the ADT List

Use the list as a data field within the class that


implements the sorted list

public class SortedList implements SortedListInterface


{ private ListInterface list;
public SortedList()
{
list = new LList();
} // end default constructor
...
} // end SortedList

13
An Implementation That Uses the ADT List

Fig. 13-6 An instance of a sorted list that


contains a list of its entries.
14
An Implementation That Uses the ADT List

Fig. 13-7 A sorted list in which Jamie belongs


after Carlos but before Sarah.

15
Efficiency Issues
ADT List Operation Array Linked
getEntry(givenPosition) O(1) O(n)
add(newPosition, newEntry) O(n) O(n)
remove(givenPosition) O(n) O(n)
contains(anEntry) O(n) O(n)
display() O(n) O(n)
clear(),getLength(),isEmpty(), O(1) O(1)
isFull()

Fig. 13-8 The worst-case efficiencies of selected ADT list


operations for array-based and linked implementations
16
Efficiency Issues
ADT List Operation Array Linked
add(newEntry) O(n) O(n2)
remove(anEntry) O(n) O(n2)
getPosition(anEntry) O(n) O(n2)
getEntry(givenPosition) O(1) O(n)
contains(anEntry) O(n) O(n)
remove(givenPosition) O(n) O(n)
display() O(n) O(n)
clear(), getLength(), O(1) O(1)
isEmpty(),isFull()

Fig. 13-9 The worst-case efficiencies of the


ADT sorted list operations when implemented
using an instance of the ADT LIST
17
Efficiency Issues

When you use an instance of an ADT list to


represent entries in ADT sorted list
• Must use the list's operations to access sorted
lists entries
• Do not access them directly

Direct access leads to inefficient


implementation of sorted list
• Underlying list uses a chain of linked nodes to
store entries
18

You might also like