1 / 21

public void print() { Iterator iter = iterator(); while(iter.hasNext())

public void print() { Iterator iter = iterator(); while(iter.hasNext()) System.out.print(iter.next()+" "); System.out.println(" "); } public class Linked_list_iterator implements Iterator { private Linked_list running; public Linked_list_iterator() {

Télécharger la présentation

public void print() { Iterator iter = iterator(); while(iter.hasNext())

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. public void print() { Iterator iter = iterator(); while(iter.hasNext()) System.out.print(iter.next()+" "); System.out.println(" "); } public class Linked_list_iterator implements Iterator { private Linked_list running; public Linked_list_iterator() { running = new Linked_list(new Link("off left",first_link())); } public boolean hasNext() { return (! running.isEmpty());} public Object next() { Object head = running.removeHead(); return head; } }

  2. public class List_driver { public static void main(String[] args) { List lst = new Linked_list(); StringTokenizer tokens = new StringTokenizer("0 1 2 3 4 5 6 7 8 9 10"); while (tokens.hasMoreTokens()) lst.addAt(0,tokens.nextToken()); ((Linked_list) lst).print(); System.out.println(lst.addAt(6,"6")); ((Linked_list) lst).print(); System.out.println(lst.removeAll("6")); ((Linked_list) lst).print(); System.out.println(lst.set(6,"x")); ((Linked_list) lst).print(); System.out.println(lst.contains("10")+ " "+ lst.contains("uuuu")); } } פלט 10 9 8 7 6 5 4 3 2 1 0 true 10 9 8 7 6 5 6 4 3 2 1 0 true 10 9 8 7 5 4 3 2 1 0 x 10 9 8 7 5 4 x 2 1 0 true false

  3. public class SortableList extends Linked_list{ public SortableList(){ super(); } private SortableList(Link first) {super(first);} private SortableList tail() { return new SortableList(first_link()); } public boolean comparableWith(Object data){ if (data == null) return false; else if (isEmpty()) return data instanceof Comparable; else return data.getClass().isInstance(head()); }

  4. public boolean addAtHead(Object data){ if (comparableWith(data)) return super.addAtHead(data); else return false; } public boolean insert(Comparable data){ // add data before the first element which is bigger than it if (! comparableWith(data)) return false; if (isEmpty()) return addAtHead(data); if (data.compareTo(head()) < 0) return addAtHead(data); return ((SortableList)tail()).insert(data); }

  5. public void sort(){ if (!isEmpty()){ Object head = removeHead(); sort(); insert((Comparable) head); } } }

  6. import java.util.StringTokenizer; public class SortableList_driver { public static void main(String[] args) { SortableList lst = new SortableList(); StringTokenizer tokens = new StringTokenizer("0 1 2 3 4 5 6 7 8 9 10"); while (tokens.hasMoreTokens()) lst.addAt(0,tokens.nextToken()); ((Linked_list) lst).print(); lst.sort(); ((Linked_list) lst).print(); } } פלט 10 9 8 7 6 5 4 3 2 1 0 0 1 10 2 3 4 5 6 7 8 9

  7. class SortedList extends SortableList{ public SortedList(){ super(); } public SortedList(SortableList list){ super(); while ( !list.isEmpty() ) insert((Comparable) list.removeHead()); } private SortedList(Link ){ super(original.first_link()); } private SortedList tail() { return new SortedList(this); }

  8. public boolean addAtHead(Object data){ Comparable d = (Comparable) data; if ((! comparableWith(d)) || ((! isEmpty()) && d.compareTo(head())> 0)) return false; return super.addAtHead(data); } public boolean addAt(int index, Object data) { Comparable d = (Comparable) data; if (index == 0) return addAtHead(data); if (isEmpty() || d.compareTo(head())< 0) return false; return tail().addAt(index-1,data); } public Object setAt(int index, Object data) { return null; } }

  9. public class SortedList_driver1 { public static void main(String[] args){ SortableList list = new SortableList(); list.addAtHead(new Integer(4)); list.addAtHead(new Integer(5)); list.addAtHead(new Integer(2)); list.addAtHead(new Integer(3)); list.addAtHead(new Integer(1)); list.addAtHead(new Integer(6)); list.print(); SortedList s = new SortedList(list); s.print(); s.insert(new Integer(4)); s.print(); s.insert(new Integer(12)); s.print(); } } פלט 6 1 3 2 5 4 1 2 3 4 5 6 1 2 3 4 4 5 6 1 2 3 4 4 5 6 12

  10. public class SortedList_driver2 { public static void main(String[] args){ SortedList l = new SortedList(); System.out.println(l.addAtHead(new Integer(3))); System.out.println(l.addAtHead(new Integer(1))); System.out.println(l.addAtHead(new Integer(3))); l.print(); System.out.println(l.addAt(3,new Integer(2))); l.print(); System.out.println(l.addAt(2,new Integer(2))); l.print(); System.out.println(l.addAt(1,new Integer(2))); l.print(); } } true true false 1 3 false 1 3 false 1 3 true 1 2 3

  11. ניפנה כעת להצגה ויישום של אלגוריתם מיון חשובmergesort האלגוריתם מעט מורכב יותר משיטות מיון שהכרנו כבר, אך עבור רשימות נתונים ארוכות הוא יעיל מהן בהרבה. mergesort – בהינתן רשימה של איברים בני השוואה - אם הרשימה ריקה החזר אותה. אחרת: א. פצל את הרשימה לשתי רשימות שוות בגודלן ( 1). ב. מיין את הרשימות בנפרד. ג. אחד את הרשימות הממוינות לרשימה ממוינת אחת, שתהיה הפלט.

  12. דוגמה נתונה רשימה של איברים בני השוואה 5 3 1 2 9 6 8 7 4 א. נפצל את הרשימה לשתי רשימות שוות בגודלן ( 1). 5 1 9 8 4 3 2 6 7 ב. מיין את הרשימות בנפרד. 9 8 5 4 1 7 6 3 2 ג. אחד את הרשימות הממוינות לרשימה ממוינת אחת. 9 8 7 6 5 4 3 2 1 הוקוס פוקוס רקורסיה

  13. מדוע זה כל כך טוב ? למען הפשטות נניח ש n , אורך הרשימה שברצוננו למיין, הוא חזקה של 2 (המסקנה נשארת נכונה גם אם אין זה נכון). פעולות הפיצול (split) ו האיחוד (merge) דורשות זמן הגדל באופן ליניארי ככל שגדלה הרשימה (זמני הפיצול והמיזוג יגדלו פי שניים כשנכפיל את אורך הרשימה). לשם הפשטות נניח שזמן הפיצול של רשימה באורך n שווה לזמן האיחוד של שתי רשימות מסודרות באורך n/2 . נקרא לזמן הזה t(n) .

  14. רשימה באורך N/4 רשימה באורך N/4 רשימה באורך N/4 רשימה באורך N/4 זמן פיצול\איחוד רשימה באורך N t(n) רשימה באורך N/2 רשימה באורך N/2 2*t(n/2) 4*t(n/4) . . . . . N רשימות באורך 1 . . . .. רשימה באורך N/4 רשימה באורך N/4 רשימה באורך N/4 רשימה באורך N/4 4*t(n/4) רשימה באורך N/2 רשימה באורך N/2 2*t(n/2) רשימה באורך N

  15. סך הכול (עד כדי כפל בקבוע) Log2(t(n)) אולם t(n) בעצמו הוא n כפול קבוע. ולכן עד כדי כפל בקבוע זמן הריצה הוא n*log(n) . מדוע לא טרחתי לציין שהמדובר בלוג על בסיס 2 ?

  16. public class Sort { public static SortableList[] split(SortableList list){ SortableList[] pair = {new SortableList(), new SortableList()}; int i = 0; while ( !list.isEmpty() ){ pair[i%2].addAtHead(list.removeHead()); i = i+1; } return pair; }

  17. public class Split_driver { public static void main(String[] args){ SortableList list = new SortableList(); list.addAtHead(new Integer(4)); list.addAtHead(new Integer(5)); list.addAtHead(new Integer(2)); list.addAtHead(new Integer(3)); list.addAtHead(new Integer(1)); list.addAtHead(new Integer(6)); list.addAtHead(new Integer(4)); list.print(); SortableList[] pair = Sort.split(list); pair[0].print(); pair[1].print(); } } פלט 4 6 1 3 2 5 4 4 2 1 4 5 3 6

  18. public static SortedList merge(SortedList list1, SortedList list2){ SortedList list; Object head; if (list1.isEmpty()) return list2; if (list2.isEmpty()) return list1; Comparable data1 = (Comparable) list1.head(); Comparable data2 = (Comparable) list2.head(); if (data1.compareTo(data2) < 0) head = list1.removeHead(); else head = list2.removeHead(); list = merge(list1,list2); list.addAtHead(head); return list; }

  19. public class Merge_driver { public static void main(String[] args){ SortedList list1 = new SortedList(); SortedList list2 = new SortedList(); list1.insert(new Integer(4)); list1.insert(new Integer(5)); list1.insert(new Integer(2)); list1.insert(new Integer(3)); list2.insert(new Integer(1)); list2.insert(new Integer(6)); list2.insert(new Integer(4)); list1.print(); list2.print(); (Sort.merge(list1,list2)).print(); } } פלט 2 3 4 5 1 4 6 1 2 3 4 4 5 6

  20. public static SortedList mergesort(SortableList list){ if (list.isEmpty() || (list.first_link().next() == null)) return new SortedList(list); SortableList[] pair = split(list); return merge(mergesort(pair[0]), mergesort(pair[1])); } } // class Sort

  21. public class SortedList_driver1 { public static void main(String[] args){ SortableList list = new SortableList(); list.addAtHead(new Integer(4)); list.addAtHead(new Integer(5)); list.addAtHead(new Integer(2)); list.addAtHead(new Integer(3)); list.addAtHead(new Integer(1)); list.addAtHead(new Integer(6)); list.addAtHead(new Integer(4)); list.print(); SortedList s = Sort.mergesort(list); s.print(); } } פלט 4 6 1 3 2 5 4 1 2 3 4 4 5 6

More Related