* * This algorithm has O(n*n) runtime efficiency for best, worst, * and average case. * * @param arr the array to be sorted. */ public static void selectionSort(int[] arr) { int smallIndex; // index of smallest element in the sublist int pass, j, n = arr.length; int temp; // pass has the range 0 to n-2 for (pass = 0; pass < n-1; pass++) { Viska Mutiawani, M.IT, Irvanizam, M.Sc, Dr. Taufik Fuadi Abidin Jurusan Informatika Universitas Syiah Kuala
Praktikum 3 Generic dan implementasi compareTo()
// scan the sublist starting at index pass smallIndex = pass; // j traverses the sublist arr[pass+1] to arr[n-1] for (j = pass+1; j < n; j++) // if smaller element found, assign smallIndex // to that position if (arr[j] < arr[smallIndex]) smallIndex = j; // swap the next smallest element into arr[pass] temp = arr[pass]; arr[pass] = arr[smallIndex]; arr[smallIndex] = temp; } } /** * A generic version of the selection sort algorithm that orders an * array of elements of type T in ascending order. The * algorithm requires that the generic type implements the * Comparable interface. * This algorithm has O(n*n) runtime efficiency for best, worst, * and average case. * * @param arr the array to be sorted. */ public static
Praktikum 3 Generic dan implementasi compareTo()
} /** * Searches the sublist [first, last) in an integer array for the * specified target value using the sequential search algorithm; * the return value is the first occurrence of a match or -1 if * not match is found. * * @param arr the integer array to be searched. * @param first starting index for the sublist. * @param last upper bound for the sublist. * @param target the search key to locate in the sublist. * * @return index of the search key, if it is contained in the * list; otherwise, -1. */ public static int seqSearch(int[] arr, int first, int last, int target) { // scan indices in the range first <= i < last; // return the index // indicating the position if a match occurs; // otherwise return -1 for (int i = first; i < last; i++) if (arr[i] == target) return i; // no return yet if match is not found; return -1 return -1; // i = last, thus no match } /** * Searches the sublist [first, last) in a specified array of * objects for the specified target value using the sequential * search algorithm; comparison is made using the * equals() method; the return value is * the first occurrence of a match or -1 if not match is found. * * @param arr the array to be searched. * @param first starting index for the sublist. * @param last upper bound for the sublist. * @param target the search key to locate in the sublist. * * @return index of the search key, if it is contained in the * list; otherwise, -1. */ public static int seqSearch(Object[] arr, int first, int last, Object target) { // scan indices in the range first <= i < last; // return the index // indicating the position if a match occurs; Viska Mutiawani, M.IT, Irvanizam, M.Sc, Dr. Taufik Fuadi Abidin Jurusan Informatika Universitas Syiah Kuala
Praktikum 3 Generic dan implementasi compareTo()
// otherwise return -1 for (int i = first; i < last; i++) if (arr[i].equals(target)) return i; // no return yet if match is not found; return -1 return -1; // i = last, thus no match } /** * Searches an integer array for the specified target value using * the binary search algorithm. The array must be sorted into * ascending order. * * @param arr the integer array to be searched. * @param first starting index for the sublist. * @param last upper bound for the sublist. * @param target the search key to locate in the sublist. * * @return index of the search key, if it is contained in the * list;otherwise, -1. */ public static int binSearch( int[] arr, int first, int last, int target) { int mid; // index of the midpoint int midValue; // object that is assigned arr[mid] int origLast = last; // save original value of last while (first < last) // test for nonempty sublist { mid = (first+last)/2; midValue = arr[mid]; if (target == midValue) return mid; // have a match // determine which sublist to search else if (target < midValue) last = mid; // search lower sublist. reset last else first = mid+1; //search upper sublist.reset first } return -1;
// target not found
}
/** * * * *
Searches the sublist [first, last) in a specified array of objects for the specified target value using the binary search algorithm; the generic type T must implement the Comparable interface with the array sorted in
Viska Mutiawani, M.IT, Irvanizam, M.Sc, Dr. Taufik Fuadi Abidin Jurusan Informatika Universitas Syiah Kuala
Praktikum 3 Generic dan implementasi compareTo()
* ascending order; comparison is made using the equals() * method; the return value is the first occurrence of a match or * -1 if not match is found. search algorithm. * * @param arr the integer array to be searched. * @param first starting index for the sublist. * @param last upper bound for the sublist. * @param target the search key to locate in the sublist. * * @return index of the search key, if it is contained in the * list; otherwise, -1. */ public static
// target not found
} /** * Returns a string that displays the elements in the array in * sequential order. The description is a comma separated list of * elements enclosed in brackets. * @return string that contains the list of elements in the array. */ // returns a string that represents an array of objects public static String toString(Object[] arr) { if (arr == null) return "null"; else if (arr.length == 0) return "[]"; // start with the left bracket String str = "[" + arr[0]; Viska Mutiawani, M.IT, Irvanizam, M.Sc, Dr. Taufik Fuadi Abidin Jurusan Informatika Universitas Syiah Kuala
Praktikum 3 Generic dan implementasi compareTo()
// append all but the last element, separating items with a comma // polymorphism calls toString() for the array type for (int i = 1; i < arr.length; i++) str += ", " + arr[i]; str += "]"; return str; } } Berikutnya kita buat class GenericSorting yang akan mengurutkan data apa saja yang ingin diurutkan, asalkan class data tersebut telah meng-implement Comparable dan override method compareTo(). public class GenericSorting { public static void main(String []ar) { Integer[] intArr = {40, 70, 50, 30}; SalaryEmployee[] emp = { new SalaryEmployee("Dunn, Moira","471-23-8092",800), new SalaryEmployee("Garcia, Avey", "398-67-1298",1200), new SalaryEmployee("Ye, Don", "682-76-1298",2000)}; Arrays.selectionSort(intArr); System.out.println("Sorted +Arrays.toString(intArr));
integers:
Arrays.selectionSort(emp); for (int i=0; i < emp.length; i++) System.out.println(emp[i].payrollCheck()); } }
LATIHAN 1 Buat semua class-class yang ada dalam praktikum ini. Apakah yang dapat anda simpulkan?
SOAL-SOAL 1.
Viska Mutiawani, M.IT, Irvanizam, M.Sc, Dr. Taufik Fuadi Abidin Jurusan Informatika Universitas Syiah Kuala
"