Binary Search and Recursion (Without recursion, get zero credit) - Create a file
ID: 3605309 • Letter: B
Question
Binary Search and Recursion (Without recursion, get zero credit) - Create a file of at least 20 student records sorted by student ID. - Read student records into a vector in C++ or Java program. (For Vector, refer to Chapter 7.11) - Student records should include Student ID, Name, GPA, Student Address, and a pointer which points to (10+) test scores for each student record located somewhere in the program or from a second file. - Display the 20+ student records (entire record, not just ID) and associated test scores. - Use recursive binary search to search three-five student-IDs in the vector. - Use recursive binary search to search a student-ID at the end of the vector. - Use recursive binary search to search a student-ID which is not in the vector. For each search, print the ID to be searched and the searched result with the found entire record. For each search, also count the number of times the recursive binary search function has gone thru the recursion cycle. And print the count for each search. Your program output must show proper information to be understood well by the reader/viewer. Binary Search and Recursion (Without recursion, get zero credit) - Create a file of at least 20 student records sorted by student ID. - Read student records into a vector in C++ or Java program. (For Vector, refer to Chapter 7.11) - Student records should include Student ID, Name, GPA, Student Address, and a pointer which points to (10+) test scores for each student record located somewhere in the program or from a second file. - Display the 20+ student records (entire record, not just ID) and associated test scores. - Use recursive binary search to search three-five student-IDs in the vector. - Use recursive binary search to search a student-ID at the end of the vector. - Use recursive binary search to search a student-ID which is not in the vector. For each search, print the ID to be searched and the searched result with the found entire record. For each search, also count the number of times the recursive binary search function has gone thru the recursion cycle. And print the count for each search. Your program output must show proper information to be understood well by the reader/viewer. Binary Search and Recursion (Without recursion, get zero credit) - Create a file of at least 20 student records sorted by student ID. - Read student records into a vector in C++ or Java program. (For Vector, refer to Chapter 7.11) - Student records should include Student ID, Name, GPA, Student Address, and a pointer which points to (10+) test scores for each student record located somewhere in the program or from a second file. - Display the 20+ student records (entire record, not just ID) and associated test scores. - Use recursive binary search to search three-five student-IDs in the vector. - Use recursive binary search to search a student-ID at the end of the vector. - Use recursive binary search to search a student-ID which is not in the vector. For each search, print the ID to be searched and the searched result with the found entire record. For each search, also count the number of times the recursive binary search function has gone thru the recursion cycle. And print the count for each search. Your program output must show proper information to be understood well by the reader/viewer.Explanation / Answer
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.java2novice.algos;
public class MyRecursiveBinarySearch {
public static int recursiveBinarySearch(int[] sortedArray, int start, int end, int key) {
if (start < end) {
int mid = start + (end - start) / 2;
if (key < sortedArray[mid]) {
return recursiveBinarySearch(sortedArray, start, mid, key);
} else if (key > sortedArray[mid]) {
return recursiveBinarySearch(sortedArray, mid+1, end , key);
} else {
return mid;
}
}
return -(start + 1);
}
public static void main(String[] args) {
int[] arr1 = {2,45,234,567,876,900,976,999};
int index = recursiveBinarySearch(arr1,0,arr1.length,45);
System.out.println("Found 45 at "+index+" index");
index = recursiveBinarySearch(arr1,0,arr1.length,999);
System.out.println("Found 999 at "+index+" index");
index = recursiveBinarySearch(arr1,0,arr1.length,876);
System.out.println("Found 876 at "+index+" index");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.