1. Implement the binarySearch algorithm presented in this chapter for an array o
ID: 3638162 • Letter: 1
Question
1. Implement the binarySearch algorithm presented in this chapter for an array of strings.2. Implement kSmall, discussed in the section "Finding the k(th) Smallest Item in an Array" as a Java method. Use the first item of the array as the pivot.
Explanation / Answer
1 #include #include using namespace std; const int SIZE = 20; string binarySearch(string [], int, int); int main() { string name[SIZE] = {"Collins, Bill", "Smith, Bart", "Michalski, Joe", "Griffen, Jim", "Sanchez, Manny", "Rubin, Sarah", "Taylor, Tyrone", "Johnson, Jill", "Allison, Jeff", "Moreno, Juan", "Wolfe, Bill", "Whitman, Jean", "Moretti, Bella", "Wu, Hong", "Patel, Renee", "Harrison, Rose", "Smith, Cathy", "Conroy, Pat", "Kelly, Sean", "Holland, Beth"}; cout > nameSearch; return 0; } string binarySearch(string name[], int size, int value) { int first = 0, last = size - 1, middle, position = -1; bool found = false; while (!found && first value) last = middle - 1; else first = middle + 1; } return position; } 2 #include using namespace std; const int SIZE = 5; int kSmall(int k, const int anArray[], int first, int last); // Returns the kth smallest value in anArray[firstt...last]. void partition(int anArray[], int size, int pivotValue, int& pivotIndex); int main() { return 0; } int kSmall(int k, int anArray[], int first, int last) { // Choose a pivot item p from anArray[first...last] int pivotIndex = first; int p = anArray[first]; // Partition the items of anArray[first...last] about p // S1= p partition(anArray, SIZE, int pivotValue, pivotIndex); if(k
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.