Question

In: Computer Science

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

[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 ternary search algorithm works exactly same as that of binary search with the only difference being instead of splitting the array into two parts it is split into three parts. The remaining functioning of the algorithm is same as that of binary search. The working program for ternary search is follows.

Program:

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

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

int main()
{
int elements,i,l,r,key;
bool p=false;
char response='Y';
cout<<"Enter number of accounts: ";
cin>>elements;
int a[elements];
l=0;
r=elements-1;
cout<<"Accounts are:\n";
for(i=0;i<elements;i++)
{
cout<<"["<<i<<"] ";
cin>>a[i];
}
sort(a,a+elements);
cout<<"\nSorted Accounts are: ";
for(i=0;i<elements;i++)
{
cout<<"\n["<<i<<"] "<<a[i];
}

while(response=='Y'||response=='y')
{
cout<<"\nEnter element to search: ";
cin>>key;
p=ternarySearch(l,r,key,a);
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;
}
}

Screenshot of the Code:

Output:

Kindly comment below for any further queries.


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...
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...
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
Correct this Binary Search (C++) // This program demostrates linear search algorithm #include <iostream> using namespace...
Correct this Binary Search (C++) // This program demostrates linear search algorithm #include <iostream> using namespace std; // Binary search algorith // f is the first , l is the last , t is the target int binarySearch(int stgrade[], int f, int l, int t) { while (f <= l) { int m = f + (l - l) / 2; // Check if x is present at mid if (stgrade[m] == t) return m; // If x greater, ignore...
Make the following assumptions: Assume a binary search tree holds integer numbers Write a pseudocode for...
Make the following assumptions: Assume a binary search tree holds integer numbers Write a pseudocode for a binary tree search algorithm that searches in the tree (starting at root) for a node which meets the following requirements, and prints a message when found: (a) has a value that is one third as large as either its left or right child node. Think carefully about what steps are needed to do the search, and review the insert and search methods for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT