/**** * * Class ListSortAndSearch contains sorting and searching algorithms for * array-based lists. The sorting algorithms are O(N2) bubble sort * and insertion, plus O(N log N) heap sort and quicksort. Two searching * algorithms are provided -- an O(N) linear search and and O(log N) binary * search. * * @author Gene Fisher * @version 22may01 * */ public class ListSortAndSearch { /** * Perform a bubble sort on the given list. */ public void bubbleSort(Object[] list) { int i, j; // Traversal indices int k; // Trace loop index Object temp; // Temp variable for swapping /* * Outta here if list is empty. */ if ((list == null) || (list.length == 0)) { return; } /* * Use a basic bubble sort algorithm. */ for (i = 0; i < list.length-1; i++) { for (j = list.length - 2; i <= j; j--) { printTraceLine("Pass " + i + "," + j + ": ", list); /* * Swap the jth and j+1th elements if jth is > j+1th */ if (((Comparable)(list[j])).compareTo(list[j+1]) > 0) { temp = list[j]; list[j] = list[j+1]; list[j+1] = temp; } } } } /** * Perform an insertion sort on the given list. */ public void insertionSort(Comparable[] list) { int i, j; // Traversal indices int k; // Trace loop index Comparable temp; // Temp variable for swapping for (i = 1; i < list.length; i++) { /* * Use a basic insertion sort algorithm. */ temp = list[i]; for (j = i; j > 0 && temp.compareTo(list[j-1]) < 0; j--) { printTraceLine("Pass " + i + "," + j + ": ", list); /* * Move a smaller element down a position. */ list[j] = list[j-1]; } list[j] = temp; } } /** * Perform an iterative linear search of the given list for the given * element. Assume the list is not null. Return the index position of the * first occurrence of the element if found, -1 otherwise. */ public int linearSearch(Object[] list, Object element) { int i; // Traversal index /* * Search this list. */ for (i = 0; i < list.length; i++) { if (element.equals(list[i])) { return i; } } /* * Return failure if not found. */ return -1; } /** * Perform an iterative binary search of the given list for the given * element. Assume the list is not null. Return the index position of the * first occurrence of the element if found, -1 otherwise. */ public int binarySearch(Object[] list, Object element) { int startPos = 0; // Initial start position int endPos = list.length - 1; // Initial end position /* * Iterate through the list while the length of the startPos/endPos * interval is >= 1 and we haven't yet found the searched-for element. * For each loop iteration, compute the interval midpoint and then do * one of the following: * * (a) If the searched-for element is at the midpoint, return * successfully. * * (b) If the searched-for element is less than the midpoint * element, search the lower half of the list. * * (c) If the searched-for element is greater than the midpoint * element, search the upper half of the list. */ while (startPos <= endPos) { int midpoint = (startPos + endPos) / 2; if (element.equals(list[midpoint])) return midpoint; else if (((Comparable)element).compareTo(list[midpoint]) < 0 ) endPos = midpoint - 1; else startPos = midpoint + 1; } /* * Fail if we never find the element. */ return -1; } /** * Make a space-delimited string out of the elements of an array-based * list. */ public String toString(Object[] list) { String result = ""; // Return result int i; // Loop index for (i = 0; i < list.length; i++) { result = result + list[i].toString() + " "; } return result; } /** * Turn tracing on or off. */ public void setTracingOn(boolean on) { tracingOn = on; } /** * If this.tracingOn is true, print a line of tracing information to * stdout. The inforation consists of the given string message followed by * a space-delimited list of the elements in the give array. This method * is called from within the sorting methods to help trace the progress of * the sort. */ protected void printTraceLine(String message, Object[] array) { if (tracingOn) { System.out.print(message + toString(array)); } } /** Method tracing is on if tracingOn == true. */ protected boolean tracingOn = false; }