Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is c++, no pointers. I just need the linear and binary search using the giv

ID: 3815485 • Letter: T

Question

This is c++, no pointers. I just need the linear and binary search using the given parameters(alogrithm). The rest I can do my own

Create a program that has two methods. Each method will pass in an array, the number of elements in the array, the search item, a number representing the number of times the array was searched and the index of the found element with the array. One method will employ either a linear search, another method will employ a binary search. Each method will keep a count of the number of times the array had to be searched. In main, after calling each method, the program will print:

Type of Search (linear or Binary), the number of times the array was search, and the index of the element you search for in the array or a -1 (indicating the element was not in the array)  

Linear search The array was searched 10 times and the element (55) was found at the array index 7.

Binary search - the array was searched 5 times and the element (66) was not in the array.

Prototype for each method

void LinearSearch (int array1[ ], int numelements, int searchitem,int &timesearched, int &arrayindex);

void BinSearch (int array2[ ], int numelements,int searchitem, int &timesearched, int &arrayindex);

Explanation / Answer

Hi, Please find my impleemtnation.

Please let me know in case of any issue.

#include <iostream>
using namespace std;

// function prototype

void LinearSearch (int array1[ ], int numelements, int searchitem,int &timesearched, int &arrayindex);
void BinSearch (int array2[ ], int numelements,int searchitem, int &timesearched, int &arrayindex);

int main(){

   return 0;
}

// function defination
void LinearSearch (int array1[ ], int numelements, int searchitem,int &timesearched, int &arrayindex){

   timesearched = 0;
   for(int i=0; i<numelements; i++){
       timesearched++;

       if(array1[i] == searchitem){
           arrayindex = i;
           return;
       }
   }

   arrayindex = -1; // not fiund in array

}
void BinSearch (int arr[ ], int numelements,int searchitem, int &timesearched, int &arrayindex){

   timesearched = 0;
   int l = 0;
   int r = numelements-1;
   while (l <= r)
   {
       timesearched++;
   int m = l + (r-l)/2;
     
   // Check if x is present at mid
   if (arr[m] == searchitem) {
       arrayindex = m;
   return;
   }
     
   // If x greater, ignore left half
   if (arr[m] < searchitem)
   l = m + 1;
     
   // If x is smaller, ignore right half
   else
   r = m - 1;
   }
     
   // if we reach here, then element was not present
   arrayindex = -1;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote