{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

BinarySearch

# BinarySearch - *Binary Search PseudoCode 1 if the code does...

This preview shows pages 1–2. Sign up to view the full content.

/*Binary Search PseudoCode 1. if the code does not have a high or low inputs create these variables low = 0 and high = array.length-1 2. then do a while loop While (low<= high) 3. Create a new variable called mid which = (high+low)/2 4. if the mid is equal to the key (element you are searching for) return mid 5. else if the key is less than mid high= mid-1 6. else low= mid+1 7. come out of the while loop and return -1 if nothing is found */ /*public int binearSearch (int[] array, int key) { int low = 0, high= array.length-1; while (low<=high) { int mid = (low + high)/2; if (array[mid] == key) { return mid; } else if (key<array[mid]) { high = mid-1; } else { low = mid+1; } return -1; }*/ public class BinarySearch { // the main method public static void main(String[] args) { // create the integer array first int[] numbers = {-5, -10, 30, 1, 5, 100, 15, 25, 0, 10}; // display the values System.out.println("----------------------"); System.out.println("Array before sorting: "); displayArray(numbers); // calls the display method

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 3

BinarySearch - *Binary Search PseudoCode 1 if the code does...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online