Question

In: Computer Science

Write a version of the binary search algorithm that can be used to search a string...

Write a version of the binary search algorithm that can be used to search a string vector object. Also, write a program to test your algorithm. (Use the selection sort algorithm you developed in Programming Exercise 12 to sort the vector.) Your program should prompt the user to input a series of strings, ending the input stream with zzz. The program should then prompt the user to search for a specific string in the list. If the string is found in the list:

 x is found in the list

else:

x is not in the list

*need answer in C++*

Solutions

Expert Solution

Algorithm

Algorithm: BinarySearch(vector<string> vs, string x)
1. Set I = 0, J = vs.size()
2. Repeat while I<=J do
3. Set Mid = (INT)((I+J)/2)
4. If  vs[mid]==x Then Goto step 6
5. If vs[mid] > x Then Set J = Mid -1
     Else Set I = Mid + 1
[End of loop]
6. If I<=J Then
       Print x + " is found in the list"
   Else
      Print x + " is not in the list"
7. Return

Program

#include <iostream>
#include <vector>
using namespace std;

//main function
int main()
{
vector<string> vs;
string st;
//prompt the user to input a series of strings
while(1)
{
cout<<"Enter a string (enter zzz to end) :";
cin >> st;
if(st=="zzz")
break;
vs.push_back(st);
}

//selection sort the list
for(int i=0; i<vs.size()-1; i++)
{
int k = i;
for(int j=i+1; j<vs.size(); j++)
{
if(vs[k]>vs[j])
k = j;
}
st = vs[i];
vs[i] = vs[k];
vs[k] = st;
}
//display the sorted list
for(int i=0; i<vs.size(); i++)
{
cout<<vs[i]<<" ";
}
cout<<endl;

// prompt the user to search for a specific string in the list.
cout<<"Enter a string to search: ";
cin >> st;

int i=0, j=vs.size()-1, mid;
//binary search
while(i<=j)
{
mid = (i+j)/2;
if(vs[mid]==st)
break;
if(vs[mid]>st)
j = mid-1;
else
i = mid+1;
}
if(i<=j)
cout<<st<<" is found in the list"<<endl;
else
cout<<st<<" is not in the list"<<endl;

return 0;
}

Output:


Related Solutions

Write an algorithm in pseudocode for the binary search method using a while loop (iterative version)...
Write an algorithm in pseudocode for the binary search method using a while loop (iterative version) The recursive ternarySearch method returns true or false depending if the element was found or not. The ternarySearch method works in a similar manner to a binary search except it uses two mid values that “divide” the array into three portions. So, it needs to consider three recursive scenarios: See sample run: Accounts are: [0] 5658845 [1] 8080152 [2] 1005231 [3] 4520125 [4] 4562555...
Write an algorithm in pseudocode for the binary search method using a while loop (iterative version)...
Write an algorithm in pseudocode for the binary search method using a while loop (iterative version) The recursive ternarySearch method returns true or false depending if the element was found or not. The ternarySearch method works in a similar manner to a binary search except it uses two mid values that “divide” the array into three portions. So, it needs to consider three recursive scenarios: See sample run: Accounts are: [0] 5658845 [1] 8080152 [2] 1005231 [3] 4520125 [4] 4562555...
Create a List object that uses the binary search algorithm to search for the string "A"....
Create a List object that uses the binary search algorithm to search for the string "A". Display a message box indicating whether the value was found. Language: C#
Question 2 (Function Template) Write a template version of the iterative binary search algorithm that searches...
Question 2 (Function Template) Write a template version of the iterative binary search algorithm that searches an array of arbitrary type for a given key. Declare and implement a class called Student that keeps the student id, name, and grade. Include a default constructor, the overloaded insertion (<<) operator and also the overloaded extraction operator (>>). Declare and implement another class called Book that keeps the book’s title, author, and price. Just like the Student class, Include in class Book...
Java String search Design and implement a recursive version of a binary search.  Instead of using a...
Java String search Design and implement a recursive version of a binary search.  Instead of using a loop to repeatedly check for the target value, use calls to a recursive method to check one value at a time.  If the value is not the target, refine the search space and call the method again.  The name to search for is entered by the user, as is the indexes that define the range of viable candidates can be entered by the user (that are...
Binary Search Algorithm a.) Create a Java application that utilizes the "Binary Search Algorithm" presented in...
Binary Search Algorithm a.) Create a Java application that utilizes the "Binary Search Algorithm" presented in chapter 19 (NOT Java's pre-built binarySearch() method from imported Java library) to search for an integer in a random array of size 30 in the range of 0 to 1000 inclusive. You should use Java's random number generator to randomize the numbers in your array. b.) The application's main() method should display unsorted array and sorted array, prompt user for a search key, allow...
Assume you need to write a Java program that uses a binary search algorithm to search...
Assume you need to write a Java program that uses a binary search algorithm to search a sorted array for a given value. 1. Write a Java pseudocode that uses recursion to accomplish the task. Here is a hint. When you are searching for a particular value in an array, there are two possible outcomes. 1) The value is found and the array index of that value is returned. 2) The value is not found and we return -1. (5...
Write a program in C++ to implement Binary Search Algorithm. Assume the data given in an...
Write a program in C++ to implement Binary Search Algorithm. Assume the data given in an array. Use number of data N at least more than 10. The function Binary Search should return the index of the search key V.
Make a Binary search program for C# and write algorithm and explain it in easy words...
Make a Binary search program for C# and write algorithm and explain it in easy words also show output and input
Write a program in C language that uses a binary search algorithm to guess a number...
Write a program in C language that uses a binary search algorithm to guess a number from 1 to 100. The computer will keep guessing until they get the users number correct.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT