public class OurSortedArrayList { // This is the array in which we'll store the integers. Integer[] data = new Integer [1]; // Initially, there are none. int numItems = 0; public void add (Integer K) { if (numItems >= data.length) { // Need more space. Let's double it. Integer [] data2 = new Integer [2 * data.length]; // Copy over data into new space. for (int i=0; i K) { k = i; break; } } // Insert at k, by shifting everything to the right. for (int j=numItems; j>k; j--) { data[j] = data[j-1]; } data[k] = K; numItems ++; } public int size () { return numItems; } public boolean contains (Integer K) { return binarySearch (data, K, 0, numItems-1); } static boolean binarySearch (Integer[] A, int value, int start, int end) { // Only need to check if the interval got inverted. if (start > end) { return false; } // Find the middle: int mid = (start + end) / 2; if (A[mid] == value) { return true; } else if (value < A[mid]) { // Search the left half: A[start],...,A[mid-1] return binarySearch (A, value, start, mid-1); } else { // Search the right half: A[mid+1],...,A[end] return binarySearch (A, value, mid+1, end); } } public String toString () { if (numItems == 0) { return "Empty"; } String s = "List: "; for (int i=0; i