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

Preview:

DESCRIPTION

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() { - PowerPoint PPT Presentation

Citation preview

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

10 9 8 7 6 5 4 3 2 1 0true10 9 8 7 6 5 6 4 3 2 1 0true10 9 8 7 5 4 3 2 1 0x10 9 8 7 5 4 x 2 1 0true false

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

פלט

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

6 1 3 2 5 41 2 3 4 5 61 2 3 4 4 5 61 2 3 4 4 5 6 12

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

פלט

10

truetruefalse1 3false1 3false1 3true1 2 3

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

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

מדוע זה כל כך טוב ?

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

) דורשות זמן הגדל באופן ליניארי ככלmerge) ו האיחוד (splitפעולות הפיצול ( שגדלה הרשימה (זמני הפיצול והמיזוג יגדלו פי שניים כשנכפיל את אורך הרשימה).

שווה לזמן האיחוד של nלשם הפשטות נניח שזמן הפיצול של רשימה באורך .n/2שתי רשימות מסודרות באורך

. t(n)נקרא לזמן הזה

14

Nרשימה באורך

N/2רשימה באורך N/2רשימה באורך

N/4רשימה באורך N/4רשימה באורך N/4 רשימה באורך N/4רשימה באורך ..... N 1 רשימות באורך

N/4רשימה באורך N/4רשימה באורך N/4 רשימה באורך N/4רשימה באורך

.

.

...

N/2רשימה באורך N/2רשימה באורך

Nרשימה באורך

זמן פיצול\איחוד

t(n)

2*t(n/2)

4*t(n/4)

4*t(n/4)

2*t(n/2)

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 44 2 1 45 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 51 4 61 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 41 2 3 4 4 5 6

פלט

Recommended