Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1. main class ( “Demo-Driver” )
- a. Create a Java file of “public class RandomAccessBinaryFileRecursiveBinarySearch”
- class containing the main method.
- b. It will create a Scanner object to receive a value into “nextShort()” from the user to
- search the file for.
- c. It will create a “File” object for “BinFileShort”.
- d. It will create a “RandomAccessFile” object to read the file data.
- e. Create “BinaryRecursiveSearch” object to call the recursive binary search method.
- f. In a loop, get a short to search for. You can use a “Y” or “N” algorithm to test for end of
- program as the binary search program did. I quickly tired of typing the “Y” and looped
- until a sentinel value was read.
- g. Invoke the binary search:
- long result = fileArray.binarySearch( randomAccessFile, 0, 4999,
- searchValue, callNumber);
- The “callNumber” allows to determine how many recursions it took. It is optional.
- h. Display whether or not the value was found.
- Enter a value to search for: 7398
- Call binarySearch. Times called 1
- 7398 was found at element 2499
- 2. Library class “BinaryRecursiveSearch”
- a. Create a Java file of “public class BinaryRecursiveSearch” class containing the “binarySearch”
- method.
- b. Check to see if the value searched for was not found. If so, return the long “-1.”
- c. Calculate the middle position of the area being searched.
- d. Resolve the byte-short alignment: “long positionValueForShort = 2 * positionValue;” .
- e. Seek that position.
- f. Read the short at that position: readShort().
- g. Recursively call the left half or right half of “binarySearch()” .
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement