public class Fib Memo public static void mainString
public class Fib. Memo{ public static void main(String[] args) { int n = 20; System. out. println("fib("+n+") = “ + fib(n)); } public static int fib(int n) { ? lookup table- איך יתבצע אתחול ה : תזכורת int[] lookup. Table = new int[n+1]; מימדי משתמשים במערך חד for (int i=0; i < lookup. Table. length ; i=i+1) 1 - תאים שלא חושבו מכילים את הערך lookup. Table[i] = -1; //EMPTY ? מה נותר לעשות לאחר האתחול return fib(lookup. Table, n); } public static int fib(int[] lookup. Table, int n) { if (n==0) ? איך תראה הפונקציה הרקורסיבית lookup. Table[n]= 0; if (n==1) lookup. Table[n]= 1; if (lookup. Table[n] == -1)//EMPTY lookup. Table[n] = fib(lookup. Table, n-1) + fib(lookup. Table, n-2); return lookup. Table[n]; } : והפתרון } 8
Memoization : כעת עץ הקריאות לפונקציה יראה כך fib(5) fib(4) fib(3) fib(1) fib(2) fib(0) fib(1) fib(0) 9
מיון מהיר ) - (Quicksort דוגמא 3 12 21 17 4 9 1 1 15 21 17 9 12 17 21 3 12 9 21 13 6 15 3 9 15 17 21 4 9 12 1 3 6 4 3 1
מיון מהיר – הקוד public class Quick. Sort { //…. public static void quicksort(int[] arr){ quicksort(arr, 0, arr. length-1); } public static void quicksort(int[] arr, int start, int end){ if (start<end){ int i = partition(arr, start, end); quicksort(arr, start, i-1); quicksort(arr, i+1, end); } } //…. 15
partition – מיון מהיר public static int partition(int[] arr, int start, int end){ int pivot = arr[start]; int i = start; int j = end; while(i<j){ while(i<end && arr[i] <= pivot) //scan upwards i=i+1; while(arr[j] > pivot) //scan downwards j=j-1; if (i<j) swap(arr, i, j); } swap(arr, start, j); //put the pivot in place return j; } אלגוריתם שמבצע שימוש במערך המקורי )ללא צורך בזכרון נוסף( נקרא In-place algorithm 17
partition דוגמאת ריצה של pivot. Swap! 6 3 15 4 i start i 1 9 3 6 3 9 12 21 17 15 4 i j i j j j end j 18
מיון מהיר – פונקציות עזר public static void init. Random. Array(int[] arr){ // shuffle the array arr int n = arr. length; for (int i = 0; i < n; i++) { arr[i] = (int) (Math. random() * 10 * n); } } public static void print. Array (int[] arr) { for (int i=0; i<arr. length; i=i+1) System. out. print (arr[i]+" "); System. out. println(); } public static void swap(int[] arr, int i, int j){ // swap arr[i] and arr[j] int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } 19
( מיון מהיר – הקוד )המשך import java. util. Scanner; public class Quick. Sort { public static void main(String[] args){ Scanner sc = new Scanner(System. in); System. out. println("Enter number of elements to sort: "); int n = sc. next. Int(); int[] arr = new int[n]; // Initializes arr with random numbers // in [0. . 10*N) init. Random. Array(arr); System. out. println("The input array: "); print. Array(arr); quicksort(arr); System. out. println("The sorted array: "); print. Array(arr); } //…. continued 20
Quicksort Example ● ● ● Video: ריקוד הונגרי רובוט חמוד http: //www. youtube. com/watch? v=2 Hjsp. VV 0 j. K 4 http: //www. youtube. com/watch? v=FSyr 8 o 8 jjw. M 21
Throw Runtime. Exception public class Main { public static void main(String[] args) { int account. Balance = 10000; account. Balance = withdraw(account. Balance, 15000); } public static int withdraw(int account. Balance, int withdrawal. Amount) { if((withdrawal. Amount >= 0)&&(withdrawal. Amount <= account. Balance)) account. Balance = account. Balance – withdrawal. Amount; else throw new Runtime. Exception("Illegal withdrawal amount"); return account. Balance; } } Output: Exception in thread "main" java. lang. Runtime. Exception: Illegal withdrawal amount at Main. withdraw(Main. java: 13) at Main. main(Main. java: 6) 24
Exceptions סוגי Exception IOException Runtime. Exception Null. Pointer. Exception 25
Throw and Catch Exceptions public class Main { public static void main(String[] args) { int account. Balance = 10000; account. Balance = withdraw( account. Balance, 15000); } public static int withdraw(int account. Balance, int withdrawal. Amount) throws Exception{ if((withdrawal. Amount >= 0)&&(withdrawal. Amount <= account. Balance)) account. Balance = account. Balance – withdrawal. Amount; else throw new Exception("Illegal withdrawal amount" ); return account. Balance; } } Compilation Error 27
Throw and Catch Exceptions public class Main { public static void main(String[] args) { int account. Balance = 10000; try{ account. Balance = withdraw(account. Balance, 15000 ); System. out. println(“Went into overdraft !"); } catch(Exception e){ System. err. println("Caught Exception: "+e. get. Message ()); } System. out. println("Current balance is "+ account. Balance +” shekels”); } public static int withdraw(int account. Balance, int withdrawal. Amount) throws Exception{ if((withdrawal. Amount >= 0)&&(withdrawal. Amount <= account. Balance)) account. Balance = account. Balance – withdrawal. Amount; else throw new Exception("Illegal withdrawal amount" ); return account. Balance; } } Output: Caught Exception: Illegal withdrawal amount Current balance is 10000 shekels 28
השלימו את הפונקציה public static String[] binary. Nums(int n){ String[] answer; if (n==0){ answer = new String[1]; answer[0]=""; } else { // // Your code here // } return answer; } 31
הרעיון של הרקורסיה 000 001 010 011 100 101 110 111 11 35 10 n=3 01 00 n=2 1 0 n=1 ”“ n=0
הקוד public static String[] binary. Nums(int n) { String[] answer; if (n==0){ answer = new String[1]; answer[0]=""; } else { String[] prev = binary. Nums(n-1); answer = new String[2*prev. length]; for (int i =0; i<prev. length; i = i+1) { answer[i] = "0" + prev[i]; answer[prev. length + i] = "1"+ prev[i]; } } return answer; } 36
קוד הדפסת הפרמוטציות של מחרוזת public static void perms(String s){ // We call the method perm(s, "") which prints // the empty string followed by each permutation // of s the empty string. perms(s, ""); } 52
קוד הדפסת הפרמוטציות של מחרוזת /** Function prints all the permutations of a string. * Note: assume the string is a set (no duplicate * chars) */ // Prints string acc followed by all permutations of // string s 1 public static void perms(String s 1, String acc){ if (s 1. length()==0) System. out. println(acc); else for (int i=0; i<s 1. length(); i=i+1) perms(s. substring(0, i) + s. substring(i+1), acc + s 1. char. At(i)); } 53
- Slides: 44