n public class Fib public static int fibint
: בסדרה n- חישוב המספר ה public class Fib { public static int fib(int n){ int ans; if (n==0) ans = 0; else if (n==1) ans = 1; else ans = fib(n-1)+fib(n-2); return ans; } public static void main(String[] args){ int n = 20; System. out. println("fib ("+n+") = "+fib(n)); } } ! לא ? רקורסית זנב 3
public class Fib. Memo{ public static void main(String[] args){ int n = 20; System. out. println("fib. Memo("+n+") = "+fib. Memo(n)); } public static int fib. Memo(int n){ int[] lookup. Table = new int[n+1]; for (int i=0; i < lookup. Table. length ; i=i+1) lookup. Table[i] = -1; //EMPTY return fib. Memo(lookup. Table, n); } public static int fib. Memo(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. Memo(lookup. Table, n-1) + fib. Memo(lookup. Table, n-2); return lookup. Table[n]; } } : והפתרון 7
מיון מהיר - (Quicksort) דוגמא 3 12 21 17 4 9 1 1 15 21 17 9 12 17 21 3 12 9 21 10 6 15 3 9 15 17 21 4 9 12 1 3 6 4 3 1
Quicksort Example • Example: • http: //www. cise. ufl. edu/~ddd/cis 3020/summer 97/lectures/lec 17/sld 001. htm • www. cs. auckland. ac. nz/software/Alg. Anim/Java/q_sort/tqs_new. html • • Video: http: //www. youtube. com/watch? v=2 Hjsp. VV 0 j. K 4 http: //www. youtube. com/watch? v=o 2 dm 4 X-t 8 L 0& http: //www. youtube. com/watch? v=FSyr 8 o 8 jjw. M 11
מיון מהיר – הקוד 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 12
( מיון מהיר – הקוד )המשך 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; } 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(); } 13
( מיון מהיר – הקוד )המשך 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); } } 14
( מיון מהיר – הקוד )המשך 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) i=i+1; while(arr[j] > pivot) j=j-1; if (i<j) swap(arr, i, j); } swap(arr, start, j); return j; } 15
השלם את הפונקציה public static String[] binary. Nums(int n){ String[] answer; if (n==0){ answer = new String[1]; answer[0]=""; } else{ // // Your code here // } return answer; } 17
רעיון של הרקורסיה 000 001 010 011 100 101 110 111 n=3 00 n=2 11 01 10 1 21 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; } 22
הפרמוטציות הדפסת : 3 דוגמה של קוד מחרוזת של 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, ""); } 29
הפרמוטציות הדפסת : 3 דוגמה של קוד מחרוזת של /** Function prints all the permutation 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(delete(s 1, i), acc +s 1. char. At(i)); } 30
delete : עזר ' פונק // This function returns the string s with the i-th // character removed public static String delete(String s, int i){ // Assumes that i is a position in the string return s. substring(0, i) + s. substring(i+1, s. length()); } 31
i=0 50: [10, 20, 30] i=1 40: [20, 30] 50: [20, 30] i=2 20: [30] 40: [30] i=3 -10 [] 20: [] 10: [] 40: [] 35
calc. Weights : 4 קוד דוגמה באמת אינו אך – הרקורסיה עבור נחוץ i הארגומנט ל הראשונה בקריאה . הבעיה של מהקלט חלק מעטפת פונקצית נוסיף לכן . 0 הוא ערכו calc. Weights. יותר פשוטה חתימה עם // An envelope function, without i argument // A simpler signature public static boolean calc. Weights(int[] weights, int sum) { return calc. Weights(weights , 0, sum); } 37
( פתרון )המשך public static boolean calc. Weights(int[] weights, int i, int sum) { boolean res = false; if (sum == 0) res = true; else if (i >= weights. length) res = false; else res = calc. Weights(weights, i+1, sum-weights[i]) || calc. Weights(weights, i + 1, sum); return res; } 38
פתרון : דוגמה אחרונה public static void subsets. Sum(int[] weights, int sum){ subsets. Sum(weights, sum, 0, ””); } public static void subsets. Sum(int[] weights, int sum, int index, String acc){ if(sum == 0) System. out. println(acc); else if (sum > 0 & index < weights. length){ subsets. Sum(weights, sum-weights[index], index+1, acc + weights[index] + ' '); subsets. Sum(weights, sum, index+1, acc); } } 41
- Slides: 41