Code for a sequential search and a binary search

Assignment Help Data Structure & Algorithms
Reference no: EM13944307

I have code for a sequential search and a binary search. I have to "add a counter for every key comparison in each search function and print out the number of key comparisons for each search."

Here is my code:

#include <iostream>
using namespace std;

int SequentialSearch(const int _list[], int _length, const int & _item);
int BinarySearch(const int _list[], int _length, const int & _item);

int main()
{
int temp;
int numbers[] = {5,10,15,20,25,30,35,40,45,50};
cout << SequentialSearch(numbers, 10, 30) << endl;
cout << BinarySearch(numbers, 10, 30) << endl;

cin >> temp;
return 0;
}
int SequentialSearch(const int _list[], int _length, const int & _item)
{
for (int x = 0; x< _length; x++)
{
if (_list[x] == _item)
return x;
}
return -1;
}
int BinarySearch(const int _list[], int _length, const int & _item)
{
int first = 0;
int last = _length -1;
int mid = 0;
bool found = false;

while (first <= last && !found)
{
mid = (first + last) / 2;
if (_list[mid] == _item)
found = true;
else if (_list[mid] > _item)
last = mid - 1;
else
first = mid + 1;
}
if (found)
return mid;
else
return -1;
}

Reference no: EM13944307

Questions Cloud

How can i verify that my algorithm has not missed : ) I only understand the first sentence of these instructions. How do I modify my program to find those 50 roots and, yes, how can I verify that my algorithm has not missed any of the roots?
Is the pharmacy likely an unrelated trade or business : An exempt municipal hospital operates a pharmacy that is staffed by a pharmacist 24 hours per day. The pharmacy serves only hospital patients. Is the pharmacy likely an unrelated trade or business? Explain.
How particular school grew of original field of epistemology : Explain who the main contributors were, and the evolution of how this particular school grew out of the original field of epistemology or metaphysics.
Confidence interval for the population mean number : Construct a 95% confidence interval for the population mean number of shares traded per day in 2010. Interpret the confidence interval.
Code for a sequential search and a binary search : I have code for a sequential search and a binary search. I have to "add a counter for every key comparison in each search function and print out the number of key comparisons for each search."
Consumer ethics problems and effect on marketing planning : Identify, and explain using examples, three consumer ethics problems and their effect on marketing planning.
Research article on congenital heart failure : Write a 2.5 page on a scholarly research article on congenital heart failure
Explain how a form becomes a window within an application. : Discuss the difference between a modal and modeless form
Discuss the egocentric predicament : Compare and contrast rationalism and empiricism in their approach to the topic of knowledge. Discuss the Egocentric Predicament. How do you solve it?

Reviews

Write a Review

Data Structure & Algorithms Questions & Answers

  Implement an open hash table

In this programming assignment you will implement an open hash table and compare the performance of four hash functions using various prime table sizes.

  Use a search tree to find the solution

Explain how will use a search tree to find the solution.

  How to access virtualised applications through unicore

How to access virtualised applications through UNICORE

  Recursive tree algorithms

Write a recursive function to determine if a binary tree is a binary search tree.

  Determine the mean salary as well as the number of salaries

Determine the mean salary as well as the number of salaries.

  Currency conversion development

Currency Conversion Development

  Cloud computing assignment

WSDL service that receives a request for a stock market quote and returns the quote

  Design a gui and implement tic tac toe game in java

Design a GUI and implement Tic Tac Toe game in java

  Recursive implementation of euclids algorithm

Write a recursive implementation of Euclid's algorithm for finding the greatest common divisor (GCD) of two integers

  Data structures for a single algorithm

Data structures for a single algorithm

  Write the selection sort algorithm

Write the selection sort algorithm

  Design of sample and hold amplifiers for 100 msps by using n

The report is divided into four main parts. The introduction about sample, hold amplifier and design, bootstrap switch design followed by simulation results.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd