Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 5 Ordered List.

Similar presentations


Presentation on theme: "Chapter 5 Ordered List."— Presentation transcript:

1 Chapter 5 Ordered List

2 Overview Linear collection of entries
All the entries are arranged in ascending or descending order of keys.

3 Learning Objectives Describe the properties of an order list.
Study binary search. Understand how a Java interface may be designed in order to ensure that an ordered list consists only of objects that may be sorted. Design the public interface of an ordered list class in Java. Learn how to merge two ordered list in an efficient manner

4 Learning Objectives Develop a list consolidation application based on merging. Implement an ordered list class in Java using an array list component.

5 5.1 Introduction Big graduation party.
Draw up an initial list of invitees. Lots of people being added or deleted or information being changed. If you don't care about the order, build an unordered list.

6 5.1 Introduction If you were to maintain the names of your invitees in alphabetical order, your program would use an ordered list.

7 5.1 Introduction The main advantage in maintaining the names in alphabetical order is that we can search for any particular name much faster than if the names were maintained in an arbitrary order. Binary search of n entries takes only O(log n) time in the worst case.

8 5.2 Binary Search Think of a number between 1 and 63.

9 5.2.2 Algorithm Guessing strategy can be translated into the binary search algorithm applied on an array in which the entries are arranged in ascending order of keys. Search for the key 19

10 5.2.2 Algorithm

11 5.2.2 Algorithm Running time analysis
The algorithm first makes one comparison to determine whether the target is equal to the middle entry. If not, one more comparison is made to go left or right. When a search terminates successfully, only one comparison (equality) is made in the last step.

12 5.2.2 Algorithm O(log n) is possible on an array, but not on a linked list. In a linked list of accessing the middle entry would take O(n) time.

13 5.4 An OrderedList Class

14 5.4 An OrderedList Class Method binarySearch
If the key is indeed in the list, the method returns the position at which the key is found. If the key does not exist in the list, the function returns a negative position whose absolute value is one more than the position at which the key would appear were it to be stored in the list.

15 5.4 An OrderedList Class

16 5.4 An OrderedList Class If we never store more than a handful of entries, we may want to go with the unordered list to avoid the overhead of data movement while not losing much by way of increased search time. If we have a large number of entries and do many more searches than insertions, then the ordered list is a clear winner.

17 [insert Operation] Find the place where the new element begins.
Create space for the new element. Put the new element on the list.

18 Original List

19 Insert Becca

20 Result

21 [delete Operation] Find the place where the element.
Delete the element. Move up all element below the deleted element one position up.

22 Original List

23 Delete Bobby

24 Keeping track of Big graduation party guest list.
Code for Keeping track of Big graduation party guest list.

25 Adding new guest name by user
OrderedList<String> graduationlist = new OrderedList<String>(); Scanner s = new Scanner(System.in); String name; System.out.println("Enter name:"); name=s.next(); graduationlist.insert(name);

26 Remove guest name from the list
System.out.println("Enter name:"); name=s.next(); graduationlist.remove(name);

27 Search for a guest name entered by user
System.out.println("Enter item name:"); name=s.next(); if(graduationlist.binarySearch(name)<0) System.out.println("name is not in the list"); else System.out.println("name is in the list");

28 Find the number of all guests
System.out.println("number of names in the list is: "+graduationlist.size());

29 Print names System.out.println("names in the list: ");
for(int i= 0; i< graduationlist.size();i++) System.out.println(graduationlist.get(i));

30 The Whole program import java.util.Scanner;
public class Big_graduation_party {   public static void main(String[] args) { boolean run = true; OrderedList<String> graduationlist = new OrderedList<String>(); while(run) {System.out.println("To add new name to the list press 1"); System.out.println("To remove name from the list press 2"); System.out.println("To search for name in the list press 3"); System.out.println("To find number of names in the list press 4"); System.out.println("To print names in the list press 5"); System.out.println("To exit press 6"); Scanner s = new Scanner(System.in); int choice = s.nextInt(); String name; switch(choice) {case 1:// code for Adding new guest name by user break; case 2:// code for Remove guest name from the list case 3:// code for Search for a guest name entered by user case 4:// code for Find the number of all guests case 5:// code for Print names case 6: run=false; break;}}} The Whole program


Download ppt "Chapter 5 Ordered List."

Similar presentations


Ads by Google