二分查找:
public class BinarySearch {
public static final int NOT_FOUND = -1;
/** * Performs the standard binary search using two comparisons per level. * * @return index where item is found, or NOT_FOUND. */ public static int binarySearch(Comparable[] a, Comparable x) { int low = 0; int high = a.length - 1; int mid;
while (low <= high) { mid = (low + high) / 2;
if (a[mid].compareTo(x) < 0) low = mid + 1; else if (a[mid].compareTo(x) > 0) high = mid - 1; else return mid; }
return NOT_FOUND; // NOT_FOUND = -1 }
// Test program public static void main(String[] args) { int SIZE = 8; Comparable[] a = new Integer[SIZE]; for (int i = 0; i < SIZE; i++) a[i] = new Integer(i * 2);
for (int i = 0; i < SIZE * 2; i++) System.out.println("Found " + i + " at " + binarySearch(a, new Integer(i)));
} }
|
快速排序:
public class QuickSort {
public static void quickSort(int[] a, int l, int r) { if (l < r) { int q = partition(a, l, r); quickSort(a, l, q - 1); quickSort(a, q + 1, r); } }
public static int partition(int[] a, int l, int r) { int pivot = a[l]; int i = l; for (int j = l + 1; j <= r; j++) { if (a[j] <= pivot) { i = i + 1; exchange(a, i, j); } } exchange(a, l, i); return i;
}
public static void exchange(int[] a, int i, int j) { int tmp = a[i]; a[i] = a[j]; a[j] = tmp; }
public static void display(int[] a) { for (int i = 0; i < a.length; i++) { System.out.print(a[i] + " "); } }
/** * @param args */ public static void main(String[] args) { int[] a = { 2, 1, 5, 3, 7, 5, 9, 0, 10, -11, 100, 20,33, 39,12,-29,-2,8 }; quickSort(a, 0, a.length-1); display(a); } } |
归并排序:
import java.util.Random;
public class MergeSort { private int[] L; private int[] R;
public void sort(int[] a, int l, int r) { int p; if (l < r) { p = (l + r) / 2; sort(a, l, p); sort(a, p + 1, r); merge(a, l, p, r); } }
private void merge(int[] a, int l, int p, int r) { int n1 = p - l + 1; int n2 = r - p;
L = new int[n1 + 1]; R = new int[n2 + 1];
for (int i = 0; i < n1; i++) { L[i] = a[l + i]; }
for (int j = 0; j < n2; j++) { R[j] = a[p + 1 + j]; }
L[n1] = Integer.MAX_VALUE; R[n2] = Integer.MAX_VALUE;
int i = 0; int j = 0; for (int k = l; k <= r; k++) { if (L[i] <= R[j]) { a[k] = L[i]; i++; } else { a[k] = R[j]; j++; } }
}
public static void main(String[] args) { int[] arr = new int[10]; Random r = new Random(); for (int i = 0; i < 10; i++) { arr[i] = r.nextInt(100); }
for (int i = 0; i < 10; i++) { System.out.print(arr[i] + " "); } System.out.println(); new MergeSort().sort(arr, 0, arr.length - 1); for (int i = 0; i < 10; i++) { System.out.print(arr[i] + " "); } System.out.println(); } } |
堆排序:
public class HeapSort {
private int heapLen;
public int[] sort(int[] array) { heapLen = array.length; buildHeap(array); // init the heap for (int i = array.length - 1; i > 0; i--) { // swap root and last // node, up to down swap(array, i, 0); heapLen--; heapify(array, 0); // reheapify the root node from 0 to n-1 } return array; }
private void buildHeap(int[] array) { for (int i = heapLen / 2; i >= 0; i--) { // down to up heapify(array, i); } }
private void heapify(int[] array, int i) { // heapify tree which root is i int left = 2 * i + 1; int right = 2 * i + 2; int largest = 0; if (left < heapLen && array[left] > array[i]) { largest = left; } else { largest = i; } if (right < heapLen && array[right] > array[largest]) { largest = right; } if (largest != i) { swap(array, i, largest); heapify(array, largest); } }
private void swap(int[] array, int i, int j) { int temp = array[i]; array[i] = array[j]; array[j] = temp; }
public static void main(String[] args) { int[] array = { 1, 21, 34, 79, 68, 2, 3, 8, 3,33, -2,7, 87, 32, 24, 6, 776 }; HeapSort heapSort = new HeapSort(); int[] result = heapSort.sort(array); for (int i = 0; i < result.length; i++) { System.out.print(result[i] + " "); } } } |