11. Selection Sort (continued) Java Programming: From Problem Analysis to Program Design, 5e public static void selectionSort( int [] list, int listLength) { int index; int smallestIndex; int minIndex; int temp; for (index = 0; index < listLength – 1; index++) { smallestIndex = index; for (minIndex = index + 1; minIndex < listLength; minIndex++) if (list[minIndex] < list[smallestIndex]) smallestIndex = minIndex; temp = list[smallestIndex]; list[smallestIndex] = list[index]; list[index] = temp; } }
17. Insertion Sort (continued) Java Programming: From Problem Analysis to Program Design, 5e public static void insertionSort( int [] list, int listLength) { int firstOutOfOrder, location; int temp; for (firstOutOfOrder = 1; firstOutOfOrder < listLength; firstOutOfOrder++) if (list[firstOutOfOrder] < list[firstOutOfOrder - 1]) { temp = list[firstOutOfOrder]; location = firstOutOfOrder; do { list[location] = list[location - 1]; location--; } while(location > 0 && list[location - 1] > temp); list[location] = temp; } } //end insertionSort
18.
19.
20.
21. Binary Search Algorithm (continued) Java Programming: From Problem Analysis to Program Design, 5e
22. Java Programming: From Problem Analysis to Program Design, 5e public static int binarySearch( int [] list, int listLength, int searchItem) { int first = 0; int last = listLength - 1; int mid; boolean found = false ; while (first <= last && !found) { mid = (first + last) / 2; if (list[mid] == searchItem) found = true ; else if (list[mid] > searchItem) last = mid - 1; else first = mid + 1; } if (found) return mid; else return –1; } //end binarySearch
23. Binary Search Algorithm (continued) Java Programming: From Problem Analysis to Program Design, 5e
24. Binary Search Algorithm (continued) Java Programming: From Problem Analysis to Program Design, 5e
25.
26.
27.
28.
29.
30.
31. Programming Example: Election Results Java Programming: From Problem Analysis to Program Design, 5e
32. Programming Example: Election Results (continued) Java Programming: From Problem Analysis to Program Design, 5e