Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

d i s c u s s i o n, you will analyze examples of search and sort routines in C++ programs. Describe when, why,

d i s c u s s i o n, you will analyze examples of search and sort routines in C++ programs. Describe when, why, and how we use search and sort routines in programming.

Instructions

  1. To get started, search the Web to find an example of a program that uses a sorting algorithm.
  2. In your initial post, describe how it works to sort data, when we use it, and why. Which sorting algorithm do you think you will use in your Capstone project?

Example :

Binary search is a very efficient searching algorithm. It works recursively, which means that the binary search function calls itself on a smaller version of the same problem.It requires elements of an array to be in sorted order.

It works like this. Given a sorted array, the item you're looking for, and the indices of the first and last elements of the array being searched, the algorithm first computes the midpoint of that array. If the item is in the array at that midpoint position, then it's found. Otherwise, the algorithm checks to see if the item is greater than or less than the item at that midpoint.

If the value of the item is less than the value of the item at the midpoint of the array, then the same binary search algorithm is applied, but only on the first half of the array. Otherwise, the binary search algorithm is applied only to the second half of the array. With each call to the function, the number of elements to search is cut in half, which is much faster than doing a linear search of every item in the array from start to finish. The total number of comparisons is actually the logarithm base 2 of the number of items in the array.

Here is the code:

#include

using namespace std;

int binarySearch(int numbers[], int item, int first, int last) {

int middle; if (first >= last) // item is not in the list of numbers return -1;

middle = (first + last)/2; // Get the midpoint of the array

if (numbers[middle] == item) return middle;

else if (numbers[middle] > item) return binarySearch(numbers, item, first, middle); return binarySearch(numbers, item, middle + 1, last); }

int main() { int numbersList[] = {1, 6, 18, 23, 35, 467, 512, 513, 604, 828}; int num, loc; cout << "Enter an integer: "; cin >> num; loc = binarySearch(numbersList, num, 0, 9);

if (loc == -1) cout << "The number " << num << " is NOT in the list "; else cout << "The number " << num << " is in the list at array position " << loc << endl;

return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Mobile Communications

Authors: Jochen Schiller

2nd edition

978-0321123817, 321123816, 978-8131724262

More Books

Students also viewed these Programming questions

Question

Write the difference between sexual and asexual reproduction.

Answered: 1 week ago

Question

What your favourite topic in mathematics?

Answered: 1 week ago

Question

Briefly describe vegetative reproduction in plants.

Answered: 1 week ago