Introduction:
The
objective of the programming exercise is to implement a binary search algorithm
to find string in
vector<string> object which is sorted by selection sort.
Program Plan:
· Define
header section.
· Define selectionSort() function.
· Define binSearch() function which
takes array arr and
string s.
o
Declare
left = 0
and right = arr.size() -1.
o
While loop
left <= right.
§ Declare m = (left + right) / 2.
§ If arr[m] == s.
· Found
element s in arr at index m, return m.
§ Else if
arr[m] < s.
· Set left = m + 1.
§ Else if arr[m] > s.
· Set right = m - 1.
o
Return -1 if not return any value in the
while loop.
· Define main() function.
o
Declare n ask to take user input.
o
Declare vector<string>
arr
of size n.
o
Take user input to fill arr.
o
Declare string s and
take user input for s.
o
Sort array using selectionSort()
o
Print sorted array
o Find index p using binSearch() function call.
o Print the result using if-else
.
0 Comments
If you have any doubt let me know.