Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Hello I have a problem regarding my c + + assignment Instructions Write a version of the binary search algorithm that can be used to
Hello I have a problem regarding my c assignment
Instructions
Write a version of the binary search algorithm that can be used to search a list of strings. Use the selection sort that you designed in Exercise to sort the list. Write a program to test the function and prompt the user to enter strings. The program should output the position of the string if found in the list and the following message if not:
x is not in the list
What I wrote with the help of someone on here:
#include
#include
#include
#include
Struct to store strings with their original positions
struct StringWithPosition
std::string str;
int originalPos;
;
Binary search function for strings
int binarySearchconst std::vector& arr, const std::string& key
int left ;
int right arr.size;
while left right
int mid left right left;
if arrmidstr key
return mid; Key found, return its position in the sorted array
if arrmidstr key
left mid ;
else
right mid ;
return ; Key not found
Selection sort function for strings with positions
void selectionSortstd::vector& arr
int n arr.size;
for int i ; i n ; i
int minIdx i;
for int j i ; j n; j
if arrjstr arrminIdxstr
minIdx j;
std::swaparri arrminIdx;
int main
std::vector strList;
std::string input;
Input strings from the user
for int i ; i ; i
std::cout "Enter string i : ;
std::cin input;
strListiinput i ;
Sort the list of strings
selectionSortstrList;
Prompt the user to enter a string to search for
std::cout "Enter a string to search for: ;
std::cin input;
Perform binary search
int result binarySearchstrList input;
if result
std::cout input is found at position strListresultoriginalPos std::endl;
else
std::cout input is not in the list." std::endl;
return ;
My rubricproblem
Status: FAILED!
Check:
Test: Binary search on an ordered list
Reason: Unable to find b is found at position in the program's output.
Enter string : Enter string : Enter string : Enter string : Enter string : Enter string : Enter string : Enter string : Enter string : Enter string : Enter a string to search for: b is found at position
Error : AssertionError Unable to find b is found at position in the program's output.
Timestamp: ::
Status: FAILED!
Check:
Test: Binary search on an unordered list
Reason: Unable to find c is found at position in the program's output.
Enter string : Enter string : Enter string : Enter string : Enter string : Enter string : Enter string : Enter string : Enter string : Enter string : Enter a string to search for: c is found at position
Error : AssertionError Unable to find c is found at position in the program's output.
Timestamp: ::
Status: PASSED!
Check:
Test: Searching for a missing item
Reason: None
Timestamp: ::
My problem is that number and are not checked but the code works. I am new to binary search so I am sorry if this is a hassle. Please take your time and remember to check if its runnable.
I had to resent the problem because my copy and paste didn't put all of it this should be the code. I am sorry to whoever is going to look at the original question realizing that it is incomplete.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started