Question

In: Computer Science

Write an algorithm in pseudocode for the binary search method using a while loop (iterative version)...

  1. 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

[5] 6545231

[6] 7895122

[7] 5552012

[8] 3852085

[9] 8777541

[10] 5050552

[11] 7576651

[12] 8451277

[13] 7825877

[14] 7881200

[15] 1302850

[16] 1250255

[17] 4581002

Sorted accounts are:

[0] 1005231

[1] 1250255

[2] 1302850

[3] 3852085

[4] 4520125

[5] 4562555

[6] 4581002

[7] 5050552

[8] 5552012

[9] 5658845

[10] 6545231

[11] 7576651

[12] 7825877

[13] 7881200

[14] 7895122

[15] 8080152

[16] 8451277

[17] 8777541

ternarySearch: element 7881200 is found true

   PASS

ternarySearch: element 7881199 is found false

   PASS

ternarySearch: element 7881201 is found false

   PASS

ternarySearch: element 2222222 is found false

   PASS

ternarySearch: element 9999999 is found false

   PASS

ternarySearch: element 0000000 is found false

   PASS

ternarySearch: element 1111111 is found false

   PASS

ternarySearch: element 1005231 is found true

   PASS

ternarySearch: element 8777541 is found true

   PASS

*** Done ***

Process finished with exit code 0

  • desired item is smaller than the element at index mid1
  • desired item is greater than the element at index mid2
  • desired item is smaller than the element at index mid2 but is greater than the element at index mid1

Utilize compareTo method, save the returned value(s) and use them in comparisons.

Use the following formulas to calculate mid indexes:

int mid1 = left + (right - left)/3;

int mid2 = right – (right - left)/3;

Solutions

Expert Solution

The following code shows the execution of ternary search to find an element in the list.

Code:

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

int ternarySearch(int l, int r, int key, int acc[])
{
if(r>=l)
{
int mid1=l+(r-l)/3;
int mid2=r-(r-l)/3;
if(acc[mid1]==key)
{
return 1;
}
if(acc[mid2]==key)
{
return 1;
}
if(key<acc[mid1])
{
return ternarySearch(l,mid1-1,key,acc);
}
else if(key>acc[mid2])
{
return ternarySearch(mid2+1,r,key,acc);
}
else
{
return ternarySearch(mid1+1,mid2-1,key,acc);
}
}
return 0;
}

int main()
{
int accounts,i,l,r,key,p=0;
char response='Y';
cout<<"Enter number of accounts: ";
cin>>accounts;
int acc[accounts];
l=0;
r=accounts-1;
cout<<"Accounts are:\n";
for(i=0;i<accounts;i++)
{
cout<<"["<<i<<"] ";
cin>>acc[i];
}
sort(acc,acc+accounts);
cout<<"\nSorted Accounts are: ";
for(i=0;i<accounts;i++)
{
cout<<"\n["<<i<<"] "<<acc[i];
}
do
{
cout<<"\nEnter account to search: ";
cin>>key;
p=ternarySearch(l,r,key,acc);
if(p==1)
{
cout<<"ternarySearch: element "<<key<<" is found true";
}
else
{
cout<<"ternarySearch: element "<<key<<" is found false";
}
cout<<"\nDo you want to continue(Y/N)? ";
cin>>response;
}while(response=='Y'||response=='y');
}


Code Screenshot:

Output Screenshot:


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...
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...
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...
Given the pseudocode for Binary Search Algorithm as below: BinarySearch(A, p, r, V)    if p...
Given the pseudocode for Binary Search Algorithm as below: BinarySearch(A, p, r, V)    if p < r q = (p + r)/2 if V = A[q] return q else if V > A[q] return BinarySearch(A, q+1, r, V)    else return BinarySearch(A, p, q-1) else if p = r,    if V = A[p]    return p else return -1    return -1 end function Using this pseudocode, write a function for BinarySearch and also complete the program, by...
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...
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...
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...
Modify the previous program to use the Do-While Loop instead of the While Loop. This version...
Modify the previous program to use the Do-While Loop instead of the While Loop. This version of the program will ask the user if they wish to enter another name and accept a Y or N answer. Remove the "exit" requirement from before. Output: Enter the full name of a person that can serve as a reference: [user types: Bob Smith] Bob Smith is reference #1 Would you like to enter another name (Y or N)? [user types: y] Enter...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT