Question

In: Computer Science

Write a program in C++ to find the number of comparisons using binarySearch and the sequential...

Write a program in C++ to find the number of comparisons using binarySearch and the sequential search algorithm as follows:

Suppose list is an array of 1000 elements.

a. Use a random number generator to fill list.

b. Use any sorting algorithm to sort list.

c. Search list for some items as follows:

i. Use the binary search algorithm to search the list. (You may need to modify the algorithm given in this chapter to count the number of comparisons.)

ii. Use the binary search algorithm to search the list, switching to a sequential search when the size of the search list reduces to less than 15. (Use the sequential search algorithm for a sorted list.)

Comments would be appreciated! Am a relatively new coder. Thank you.

Solutions

Expert Solution

The Code below will help you generate the required output.

main.cpp

#include <iostream>

using namespace std;


// static function to count number of comparisons
static int count = 0;
//function for linear search or sequential search

int search(int arr[], int n, int x)
{
count++;
int i;
for (i = 0; i < n; i++)
if (arr[i] == x)
return i;
return -1;
}
  

// utility function to swap two numbers
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
  
// Bubble sort : sorting array
void bubbleSort(int arr[], int n)
{
int i, j;
for (i = 0; i < n-1; i++)
  
for (j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
swap(&arr[j], &arr[j+1]);
}

// Function to print an array
void printArray(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}


int binarySearch(int arr[], int l, int r, int x)
{
count++;
if (r >= 15) {
int mid = l + (r - l) / 2;
  
// If the element is present at the middle
if (arr[mid] == x)
return mid;
  
// If element is smaller than mid, then search lst sub array
if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x);
  
// Else the element can only be present in right sub array
return binarySearch(arr, mid + 1, r, x);
}
else // switch to sequential search
{
return search(arr,(r-l),x);
}

// Not in array of numbers
return -1;
}

int main()
{
//variables
int numberOfElements = 1000;
int *numbers = new int[numberOfElements];
int x;
// Use current time as seed for random generator
srand(time(0));
  
// generating numbers between 1 and 10000
for(int i = 0; i<numberOfElements; i++)
numbers[i] = rand() % 10000 + 1;

bubbleSort(numbers,numberOfElements);   
//displaying array
printArray(numbers,numberOfElements) ;
  
// user input to search element
cout<<"element to Search : " ;
cin>> x;
  
// calling function to search x
int result = binarySearch(numbers, 0, numberOfElements-1, x);
  
cout<<"Number of Comparisons : "<<count<<endl;
cout<<"Position of element in array is : "<<result<<endl;
}

OUTPUT


Related Solutions

IN C++ Write a program to find the number of comparisons using binarySearch and the sequential...
IN C++ Write a program to find the number of comparisons using binarySearch and the sequential search algorithm as follows: Suppose list is an array of 1000 elements. 3 Search list for some items as follows: a. Use the binary search algorithm to search the list. (You may need to modify the algorithm given in this chapter to count the number of comparisons.) b. Use the binary search algorithm to search the list, switching to a sequentialsearch when the size...
IN C++ Write a program to find the number of comparisons using binarySearch and the sequential...
IN C++ Write a program to find the number of comparisons using binarySearch and the sequential search algorithm as follows: Suppose list is an array of 1000 elements. 2 Use any sorting algorithm to sort list.
In C++ Write a program to find the number of comparisons using binarySearch and the sequential...
In C++ Write a program to find the number of comparisons using binarySearch and the sequential search algorithm as follows: Suppose list is an array of 1000 elements. 5.1 Use a random number generator to fill list.
Question 5 Write a program to find the number of comparisons using binarySearch and the sequential...
Question 5 Write a program to find the number of comparisons using binarySearch and the sequential search algorithm as follows: Suppose list is an array of 1000 elements. 5.1 Use a random number generator to fill list. 5.2 Use any sorting algorithm to sort list. 5.3 Search list for some items as follows: a. Use the binary search algorithm to search the list. (You may need to modify the algorithm given in this chapter to count the number of comparisons.)...
In JAVA!!! write a method called Comparisons that will return the number of comparisons to find...
In JAVA!!! write a method called Comparisons that will return the number of comparisons to find an element in the tree. The main program calls this method for each element, adding the comparisons each time in order to count the total number of comparisons. The program then outputs the total number of comparisons and the average number. You may use the program BuildTreeWIthMethod to build your tree. Then, after you have made the call to inOrder(root), add the following code:...
write a method called Comparisons that will return the number of comparisons to find an element...
write a method called Comparisons that will return the number of comparisons to find an element in the tree. The main program calls this method for each element, adding the comparisons each time in order to count the total number of comparisons. The program then outputs the total number of comparisons and the average number. You may use the program BuildTreeWIthMethod to build your tree. Then, after you have made the call to inOrder(root), add the following code: int totalComparisons=0;...
write C++ program using functions (separate function for each bottom) Write a program to find if...
write C++ program using functions (separate function for each bottom) Write a program to find if a number is large word for two given bottom base - bottom1 and bottom2. You can predict that a number, when converted to any given base shall not exceed 10 digits. . the program should ask from user to enter a number that it should ask to enter the base ranging from 2 to 16 after that it should check if the number is...
Write a C++ program to find the number of pairs of integers in a given array...
Write a C++ program to find the number of pairs of integers in a given array of integers whose sum is equal to a specified number.
• Write a C++ program to find the largest umber, smallest number and sum of all...
• Write a C++ program to find the largest umber, smallest number and sum of all the element of a given array of 20 integers • Note − Declare array to 20 numbers − Input values for 20 array elements − Find largest number − Find smallest number − Find sum of all numbers • Display the results
Write a C program to find out the number of words in an input text file...
Write a C program to find out the number of words in an input text file (in.txt). Also, make a copy of the input file. Solve in C programming.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT