Presentation is loading. Please wait.

Presentation is loading. Please wait.

ISOM MIS 215 Module 1 – Ordered Lists. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.

Similar presentations


Presentation on theme: "ISOM MIS 215 Module 1 – Ordered Lists. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners."— Presentation transcript:

1 ISOM MIS 215 Module 1 – Ordered Lists

2 ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners MIS215 Binary Search Search Techniques Sorting Techniques Bubblesort Basic Algorithms Fast Sorting algos (quicksort, mergesort) Hashtables Graphs, Trees Linked Lists Stacks, Queues List StructuresAdvanced structures

3 ISOM Today’s buzzwords Ordered Lists  Lists that need to be kept ordered for whatever semantic reason  We always assume that order is based on a key Templates  A special Object-Oriented technique by which collections can be created which do not have a “hardcoded” content type

4 ISOM Primary difference Items must have a (partial) ordering Orders among basic types is simple using Orders among objects are achieved in Java using the “Comparable” interface  compareTo  equals So… how do you make a list of objects?

5 ISOM Templates in Java Problem: You want to store an object in a list. Examples: So should you create a separate list class for every object? Solution: Template

6 ISOM How do you use a Template? /** * Turning our good old Mylist to a template */ public class MyTemplateList { // The array to store the values private T datastore[]; private final int MAX = 50; private int numelements =0; /** * The MyList Constructor - we may need variants later */ public MyTemplateList() { datastore = (T[])(new Object[MAX]); } … /** * The find method - takes a target and returns true if item is in the list **/ public boolean find(T target) { for(int i=0; i < this.size(); i++) if (datastore[i].equals(target)) return true; return false; } /** * Insert method - insert a new element * returns true if insert was successful, false otherwise **/ public boolean insert(T x) { if (isListFull()) return false; datastore[numelements] = x; numelements ++; return true; }

7 ISOM And how do we use it? To use it with integers: MyTemplateList l = new MyTemplateList (); l.insert(new Integer(50)); l.insert(new Integer(35)); l.insert(new Integer(75)); l.insert(new Integer(532)); System.out.println("List empty returns: " + l.isListEmpty()); System.out.println("List full returns: " + l.isListFull()); System.out.println("List size returns: " + l.size()); l.traverse(); To use it with strings: MyTemplateList lstr = new MyTemplateList (); lstr.insert("John"); lstr.insert("Jill"); lstr.insert("Mike"); lstr.insert("Mary"); lstr.traverse(); System.out.println("find(John) - item in the list - returns: " + lstr.find("John")); System.out.println("find(Tom) - item not in the list - returns: " + lstr.find("Tom"));

8 ISOM Exercise: How would you turn MyLinkedList to Templates?

9 ISOM Lets get back to Ordered Lists Think array implementation for now How would insert change? How would find change?

10 ISOM What about complexity? Complexity of _____________ A.O(1) … i.e., Constant time B.O(lg n) … faster than linear! C.O(n) … i.e., proportional to #items D.More than O(n)?

11 ISOM The binary search algorithm Since the list is ordered, there is no point starting at the beginning Lets start at the middle  If target is > item where would it be?  Otherwise? What do we achieve? Whats the worst case number of comparisons, for, say, 15 items?

12 ISOM Build this table for lists OperationUnordered ListOrdered List create/constructorO(1) size clear traverse isListFull isListEmpty insert remove find findMin findMax findAvg

13 ISOM Points to Ponder Would it make sense to implement an ordered list using a Linked List?


Download ppt "ISOM MIS 215 Module 1 – Ordered Lists. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners."

Similar presentations


Ads by Google