Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sorted Lists Chapter 13. 2 Chapter Contents Specifications for the ADT Sorted List Using the ADT Sorted List A Linked Implementation The Method add The.

Similar presentations


Presentation on theme: "Sorted Lists Chapter 13. 2 Chapter Contents Specifications for the ADT Sorted List Using the ADT Sorted List A Linked Implementation The Method add The."— Presentation transcript:

1 Sorted Lists Chapter 13

2 2 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

3 3 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 Remove an entry 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 Note: a sorted list will not let you add or replace an entry by position

4 4 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... private class Node { private Object data; private Node next;... < Accessor and mutator methods: getData, setData, getNextNode, setNextNode... } // end Node } // end SortedLinkedList

5 5 The Method add Fig. 13-1 Insertion points of names into a sorted chain of linked nodes.

6 6 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()

7 7 The Method add Fig. 13-2 Recursively adding Luke to a sorted chain of names

8 8 The Method add Fig. 13-3 Recursively adding a node at the beginning of the chain … continued →

9 9 The Method add Fig. 13-3 (ctd) Recursively adding a node at the beginning of the chain.

10 10 The Method add Fig. 13-4 Recursively adding a node between existing nodes in a chain … continued →

11 11 The Method add Fig. 13-4 (ctd) Recursively adding a node between existing nodes in a chain.

12 12 Efficiency of the Linked Implementation ADT Sorted List Operation Array Linked add(newEntry) remove(anEntry) getPosition(anEntry) getEntry(givenPosition) contains(anEntry) remove(givenPosition) display() clear(), getLength(), isEmpty(), isFull() O(n) O(1) O(n) O(1) O(n) O(1) Fig. 13-5 The worst-case efficiencies of the operations on the ADT sorted list for two implementations

13 13 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

14 14 An Implementation That Uses the ADT List Fig. 13-6 An instance of a sorted list that contains a list of its entries.

15 15 An Implementation That Uses the ADT List Fig. 13-7 A sorted list in which Jamie belongs after Carlos but before Sarah.

16 16 Efficiency Issues ADT List Operation Array Linked getEntry(givenPosition) add(newPosition, newEntry) remove(givenPosition) contains(anEntry) display() clear(),getLength(),isEmpty(), isFull() O(1) O(n) O(1) O(n) O(1) Fig. 13-8 The worst-case efficiencies of selected ADT list operations for array-based and linked implementations

17 17 Efficiency Issues ADT List Operation Array Linked add(newEntry) remove(anEntry) getPosition(anEntry) getEntry(givenPosition) contains(anEntry) remove(givenPosition) display() clear(), getLength(), isEmpty(),isFull() O(n) O(1) O(n) O(1) O(n 2 ) O(n) O(1) Fig. 13-9 The worst-case efficiencies of the ADT sorted list operations when implemented using an instance of the ADT LIST

18 18 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


Download ppt "Sorted Lists Chapter 13. 2 Chapter Contents Specifications for the ADT Sorted List Using the ADT Sorted List A Linked Implementation The Method add The."

Similar presentations


Ads by Google