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;...
Please write program in c++ with using pointer. create a function that can find the number...
Please write program in c++ with using pointer. create a function that can find the number of even numbers that are between the maximum and minimum elements of an given array.The program have to use pointer. example 8 -1 2 2 1 9 2 4 0 output: 2
Write a C code program to implement the comparisons of three integer numbers, using only conditional...
Write a C code program to implement the comparisons of three integer numbers, using only conditional operators (no if statements). Please ask the user to input random three integers. Then display the minimum, middle, and maximum number in one line. Also, please calculate and display the sum and the average value (save two digits after decimal point) of these three integers. Please write the comments line by line.
c++ program that finds the number of comparisons of 1000 random numbers using  bubble, insertion, quick, shell,...
c++ program that finds the number of comparisons of 1000 random numbers using  bubble, insertion, quick, shell, selection, and merge sort.
Write a parallel program using Pthread based on given sequential solution. Please set the thread number...
Write a parallel program using Pthread based on given sequential solution. Please set the thread number as 10 in your code. Given Sequential Solution: #include <stdlib.h> #include <stdio.h> #include <string.h> #define MAX 10240 int total = 0; int n1,n2; char *s1,*s2; FILE *fp; int readf(FILE *fp) { if((fp=fopen("strings.txt", "r"))==NULL){ printf("ERROR: can't open string.txt!\n"); return 0; } s1=(char *)malloc(sizeof(char)*MAX); if(s1==NULL){ printf("ERROR: Out of memory!\n"); return -1; } s2=(char *)malloc(sizeof(char)*MAX); if(s2==NULL){ printf("ERROR: Out of memory\n"); return -1; } /*read s1 s2 from...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT