Stack - Invariant Algorithm maintains a[lo ≤ key ≤ a[hi-1 Java library implementation Arrays.binarySearch public static int search(String key

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
public class Stack<Item> { private Node first = null; private class Node { private Item item; private Node next;} public boolean isEmpty() { return first == null; } public void push(Item item) { Node second = first; first = new Node(); first.item = item; = second;} public Item pop() { Item item = first.item; first =; return item; } } public static void quicksort(double[] a, int left, int right) { if (right <= left) return; int i = partition(a, left, right); quicksort(a, left, i-1); quicksort(a, i+1, right); } Binary Search: Java Implementation
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Invariant. Algorithm maintains a[lo] ≤ key ≤ a[hi-1]. Java library implementation: Arrays.binarySearch() public static int search(String key, String a) { return search(key, a, 0, a.length); } public static int search(String key, String a, int lo, int hi) { if (hi <= lo) return -1; int mid = lo + (hi - lo) / 2; int cmp = a[mid].compareTo(key); if (cmp > 0) return search(key, a, lo, mid); else if (cmp < 0) return search(key, a, mid+1, hi); else return mid; }...
View Full Document

This note was uploaded on 04/07/2008 for the course ACCP 101 taught by Professor Whoeveraccepts during the Fall '07 term at UVA.

Ask a homework question - tutors are online