Class Sorter static void Sort int arr static

  • Slides: 25
Download presentation

Class Sorter • static void Sort (int[ ] arr) • static void Sort (double[]

Class Sorter • static void Sort (int[ ] arr) • static void Sort (double[] arr) • static void Sort (string[] arr) • static void Sort (Date[] arr) © המרכז להוראת המדעים האוניברסיטה העברית בירושלים מיון : המשימה 2

selection sort – מיון : המשימה © המרכז להוראת המדעים האוניברסיטה העברית בירושלים public

selection sort – מיון : המשימה © המרכז להוראת המדעים האוניברסיטה העברית בירושלים public static void Sort(int[] arr){ // the index of the minimum int min. Index; //while the unsorted part is not last place in list: for (int place. Index = 0; place. Index < arr. length-1; place. Index++){ min. Index = find. Min. Index(arr, place. Index); swap(arr, min. Index, place. Index); } //end of for } //end of method 4

//swaps the arr[index 1] with arr[index 2] private static void Swap (int[] arr, int

//swaps the arr[index 1] with arr[index 2] private static void Swap (int[] arr, int index 1, int index 2){ int temp = arr[index 1]; arr[index 1] = arr[index 2]; arr[index 2] = temp; } //search for the index of smallest number in the unsorted part of list: private static int Find. Min. Index(int[] arr, int place. Index){ int minimum = arr[place. Index]; int min. Index = place. Index; for(int i = place. Index+1; i < arr. Length; i++){ if (arr[i]<minimum){ minimum = arr[i]; min. Index = i; } } return min. Index; } © המרכז להוראת המדעים האוניברסיטה העברית בירושלים selection sort – מיון : המשימה 5

selection sort – מיון : המשימה /** This is a helper class that helps

selection sort – מיון : המשימה /** This is a helper class that helps to sort arrays */ public class Sorter{ … /** sorts an array of integers from top to bottom */ … } /** sorts an array of Strings by lexicographical order */ public static void Sort(string[ ] arr){ … } © המרכז להוראת המדעים האוניברסיטה העברית בירושלים public static void Sort (int[] arr){ 6

(string[] public static void Sort(int [] arr){ int min. Index; //while the unsorted part

(string[] public static void Sort(int [] arr){ int min. Index; //while the unsorted part is not last place in list: for (int place. Index = 0; place. Index < arr. Length-1; place. Index++){ min. Index = Find. Min. Index(arr, place. Index); Swap(arr, min. Index, place. Index); } //end of for } //end of method © המרכז להוראת המדעים האוניברסיטה העברית בירושלים מיון מחרוזות 7

selection sort – מיון : המשימה //swaps the arr[index 1] with arr[index 2] private

selection sort – מיון : המשימה //swaps the arr[index 1] with arr[index 2] private static void Swap ((string[] int[] arr, int index 1, int index 2){ int temp = arr[index 1]; string } //search for smallest number in the unsorted part of list: private static int Find. Min. Index((string[] int[] arr, int index){ int minimum = arr[place. Index]; string int min. Index = place. Index; for(int i= place. Index; i<arr. length; i++){ if (arr[i] << minimum){ ? ? ? minimum = arr[i]; min. Index =i; } return min. Index; } © המרכז להוראת המדעים האוניברסיטה העברית בירושלים arr[index 1] = arr[index 2]; arr[index 2] =temp; 8

. . . המטרה ברקע Sort(String[ ] arr) static void Sort(Comparable[ ] arr) Sort(double[

. . . המטרה ברקע Sort(String[ ] arr) static void Sort(Comparable[ ] arr) Sort(double[ ] arr) Sort(int[ ] arr) © המרכז להוראת המדעים האוניברסיטה העברית בירושלים Sort(Date[ ] arr) 10

public class Date : Comparable{ private int day; private int month; הממשק אותו private

public class Date : Comparable{ private int day; private int month; הממשק אותו private int year; המחלקה מממשת … public bool Is. Equal (Object o){ Date d 2 = (Date)o; return (d 2. day = = this. day) && (d 2. month = = this. month) &&(d 2. year = = this. year); } … } © המרכז להוראת המדעים האוניברסיטה העברית בירושלים מימוש הממשק : דוגמה 14

public static void Sort(Comparable[] arr){ int min. Index; //while the unsorted part is not

public static void Sort(Comparable[] arr){ int min. Index; //while the unsorted part is not last place in list: for (int place. Index = 0; place. Index < arr. length-1; place. Index++){ min. Index = Find. Min. Index (arr, place. Index); Swap (arr, min. Index, place. Index); } //end of for } //end of method © המרכז להוראת המדעים האוניברסיטה העברית בירושלים מיון בגירסה כללית : ממשקים ופולימורפיזם 19

//swaps the arr[index 1] with arr[index 2] private static void Swap (Comparable[] arr, int

//swaps the arr[index 1] with arr[index 2] private static void Swap (Comparable[] arr, int index 1, int index 2){ Comparable temp = arr[index 1]; arr[index 1] = arr[index 2]; arr[index 2] = temp; } //search for smallest number in the unsorted part of list: private static int Find. Min. Index (Comparable[] arr, int place. Index){ Comparable minimum = arr[place. Index]; int min. Index = place. Index; for (int i = place. Index; i<arr. length; i++){ if (arr[i]. Is. Less. Than(minimum)){ minimum = arr[i]; min. Index = i; } return min. Index; } © המרכז להוראת המדעים האוניברסיטה העברית בירושלים מיון בגירסה כללית : ממשקים ופולימורפיזם 20

! המטרה הושגה Sort. Strings(String[ ] names) static void Sort(Comparable[ ] arr) Sort. Double(double[

! המטרה הושגה Sort. Strings(String[ ] names) static void Sort(Comparable[ ] arr) Sort. Double(double[ ] arr) Sort. Ints(int[ ] arr) © המרכז להוראת המדעים האוניברסיטה העברית בירושלים Sort. Dates(Date[ ] dates) 21

public static void Main (string[] args){ string[] str. Arr = {“Yoel”, ”Sara”, “Moshe”}; Sorter.

public static void Main (string[] args){ string[] str. Arr = {“Yoel”, ”Sara”, “Moshe”}; Sorter. Sort(str. Arr); for (int i = 0; i < str. Arr. length ; i++){ Console. Write. Line (str. Arr[i] ); } } © המרכז להוראת המדעים האוניברסיטה העברית בירושלים : שימוש בשיטה הכללית 22