Presentation is loading. Please wait.

Presentation is loading. Please wait.

יסודות מדעי המחשב – תרגול 4

Similar presentations


Presentation on theme: "יסודות מדעי המחשב – תרגול 4"— Presentation transcript:

1 יסודות מדעי המחשב – תרגול 4
מיונים ורקורסיה

2 מהלך התרגול המשך מיונים בדיקת קלט משחקי מצביעים רקורסיות "פשוטות"
חיפוש רקורסיבי

3 מיון מחרוזות שימו לב – המחלקה Sort נמצאת בחומר עזר באתר
public class sortExample { /** args */ public static void main(String[] args) { String[] strArr = {"g","r","a","e","hello","world","bungee","alligator","java","gray","bun","t", "c","grab"}; char[] charArr = {'g','c','e','a','r','s','z','l'}; System.out.println("printing string array"); printArray(strArr); Sort.strLengthSort(strArr); System.out.println("printing length sorted array"); Sort.stringSort(strArr); System.out.println("printing lexicographicly sorted array"); System.out.println("printing character array"); printArray(charArr); Sort.charSort(charArr); System.out.println("printing sorted character array"); } public static void printArray(String[] arr) { int i; for (i=0;i<arr.length;i++) System.out.println(arr[i]); } public static void printArray(char[] arr) System.out.print(arr[i]); System.out.print("."); System.out.println(); שימו לב – המחלקה Sort נמצאת בחומר עזר באתר

4 בדיקת קלט public static void printArray(String[] arr) { int i;
for (i=0;i<arr.length;i++) System.out.println(arr[i]); } public static void printArray(char[] arr) System.out.print(arr[i]); System.out.print("."); System.out.println(); public static void printArray(String[] arr) { if (arr!= null) int i; for (i=0;i<arr.length;i++) System.out.println(arr[i]); } else System.out.println(“Cannot print null array.”); public static void printArray(char[] arr) System.out.print(arr[i]); System.out.print("."); System.out.println();

5 משחקי מצביעים public static void switchString(String s1, String s2) { String str = s1; s1 = s2; s2 = str; } public static void swapArray(int[] arr1, int[] arr2) int[] tmp = arr1; arr1 = arr2; arr2 = tmp; public static int swap(int[] arr1, int[] arr2, int begin) { int tmp; if (begin < Math.min(arr1.length, arr2.length)) tmp = arr1[begin]; arr1[begin] = arr2[begin]; arr2[begin] = tmp; return swap(arr1, arr2, begin+1)+1; } else return 0;

6 Fibonacci סדרת פיבונצ'י מוגדרת עבור n>=2 בצורה הבאה:
F(n) = F(n-1) + F(n-2) כאשר על פי ההגדרה F(1)=1 ו F(0)=0 public static long F(int n) { if (n == 0) return 0; } else if (n == 1) return 1; else return F(n-1) + F(n-2);

7 עוד רקורסיה – מה היא עושה?

8 חיפוש בינרי מטרה: חיפוש איבר במערך ממוין.


Download ppt "יסודות מדעי המחשב – תרגול 4"

Similar presentations


Ads by Google