Chapter 5 Ordered List.

Slides:



Advertisements
Similar presentations
Recursion Chapter 14. Overview Base case and general case of recursion. A recursion is a method that calls itself. That simplifies the problem. The simpler.
Advertisements

Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
Searching and Sorting I 1 Searching and Sorting 1.
Searching Algorithms. Lecture Objectives Learn how to implement the sequential search algorithm Learn how to implement the binary search algorithm To.
Chapter 8 Lists. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-2 Chapter Objectives Examine list processing and various ordering techniques.
1 CSC 222: Computer Programming II Spring 2005 Searching and efficiency  sequential search  big-Oh, rate-of-growth  binary search  example: spell checker.
Chapter 8 ARRAYS Continued
Chapter 5 Ordered List. Overview ● Linear collection of entries  All the entries are arranged in ascending or descending order of keys.
Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques.
Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 12 l Basics of Recursion l Programming with Recursion Recursion.
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.
CS 162 Intro to Programming II Searching 1. Data is stored in various structures – Typically it is organized on the type of data – Optimized for retrieval.
CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.
CSC 211 Data Structures Lecture 13
Data Structures and Algorithms Lecture 1 Instructor: Quratulain Date: 1 st Sep, 2009.
CS261 Data Structures Ordered Bag Dynamic Array Implementation.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
1 Applied Arrays Lists and Strings Chapter 12 2 Applying What You Learn Searching through arrays efficiently Sorting arrays Using character arrays as.
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
1 Chapter 4 Unordered List. 2 Learning Objectives ● Describe the properties of an unordered list. ● Study sequential search and analyze its worst- case.
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Searching algorithms with simple arrays
Using recursion for Searching and Sorting
Sorts, CompareTo Method and Strings
Chapter 4 Unordered List.
The List ADT.
Recursion Topic 5.
Chapter 5 Ordered List.
Searching.
11 Map ADTs Map concepts. Map applications.
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Chapter 11 Heap.
Data Structures I (CPCS-204)
Lecture 3 Linear Search and Binary Search ArrayLists
Big-O notation Linked lists
Searching Given a collection and an element (key) to find… Output
Chapter 5 Ordered List.
Chapter 6 Queue.
CSC 222: Object-Oriented Programming
Recitation 13 Searching and Sorting.
Efficiency of in Binary Trees
An Introduction to Sorting
Binary Tree and General Tree
Binary Search Trees.
Top Ten Words that Almost Rhyme with “Peas”
Arrays … The Sequel Applications and Extensions
Chapter 8: Collections: Arrays
Chapter 4 Unordered List.
MSIS 655 Advanced Business Applications Programming
Object Oriented Programming in java
Chapter 6 Queue.
Chapter 5 Ordered List.
A Robust Data Structure
24 Searching and Sorting.
Data Structures (CS212D) Week # 2: Arrays.
Chapter 6 Queue.
Data Structures Sorted Arrays
Basics of Recursion Programming with Recursion
Introduction to Data Structures
Searching and Sorting Arrays
Chapter 4 Unordered List.
The Generic List<> Collection class
Data Structures & Algorithms
CSE 143 Lecture 2 ArrayIntList, binary search
Sorting and Searching -- Introduction
Chapter 19 Searching, Sorting and Big O
Presentation transcript:

Chapter 5 Ordered List

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

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

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

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.

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

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.

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

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

5.2.2 Algorithm

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.

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.

5.4 An OrderedList Class

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.

5.4 An OrderedList Class

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.

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

Original List

Insert Becca

Result

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

Original List

Delete Bobby

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

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

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

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");

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

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

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