Question

In: Computer Science

You are an analytics developer, and you need to write the searching algorithm to find the...

You are an analytics developer, and you need to write the searching algorithm to find the element. Your program should perform the following: Implement the Binary Search function. Write a random number generator that creates 1,000 elements, and store them in the array. Write a random number generator that generates a single element called searched value. Pass the searched value and array into the Binary Search function. If the searched value is available in the array, then the output is “Element found,” or else the output is “Not found.” This question must be done in Code Blocks for C++

Solutions

Expert Solution

#include <iostream>

#include <stdio.h>

#include <stdlib.h>

using namespace std;
#define SIZE 1000
void bubbleSort(int array[],int n) {

       int i, j, temp;
       for (i = 0; i < n - 1; ++i) {

           for (j = 0; j < n - 1 - i; ++j) {

               if (array[j] > array[j + 1]) {

                   temp = array[j + 1];

                   array[j + 1] = array[j];

                   array[j] = temp;
               }

           }

       }
   }

   // performs binary search on the array and returns the number of
   // comparisions
   int binarySearch(int arr[], int l, int r, int x, int comp) {

       if (r >= l) {
          
           int mid = l + (r - l) / 2;
           if (arr[mid] == x) {
               return mid;
           }
           if (arr[mid] < x)
               return binarySearch(arr, mid+ 1, r, x, comp);

           return binarySearch(arr, l, mid - 1, x, comp);
       }
       return -1;
   }

   int main() {
       int *a= new int[SIZE];
  
       for (int i = 0; i < SIZE; ++i) {
           a[i] = rand()%1000;
       }
       bubbleSort(a,SIZE);
  
       // generating element to search in the array
       int ele=rand()%1000;
      
       // calling binary search
       if(binarySearch(a, 0, SIZE, ele, 0)==-1)
   cout<<"Not found";
   else
   cout<<"Element found";
   }

  

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me

Not found ... Program finished with exit code 0 Press ENTER to exit console.


Related Solutions

You are working as the software developer and need to identify the best sorting algorithm. You...
You are working as the software developer and need to identify the best sorting algorithm. You can pick any two-sorting algorithm. You need to determine which sorting algorithm is the best to sort the array of 10k elements. Use the following steps to help with your solution: Create C++ functions for any two-sorting algorithm. Write down the random array generation function to generate at least 10k elements. Pass the same array into both the sorting algorithm. Calculate the end transactiontime-start...
Using Java implement a searching algorithm to solve the following problem (please specify the searching algorithm...
Using Java implement a searching algorithm to solve the following problem (please specify the searching algorithm being used) Requirements Choose one problem with an algorithm and implement it. You should show and explain the result whatever you got. I recommend using N-Queen problem (at least N=8 or more) or any simple perfect games. For example, - N-Queen problem with hill climbing - N-Queen problem with simulated annealing - N-Queen problem with genetic algorithm - Tic-Tac-Toe with Minimax
Using Java implement a searching algorithm to solve the following problem (please specify the searching algorithm...
Using Java implement a searching algorithm to solve the following problem (please specify the searching algorithm being used) N-Queen problem with genetic algorithm Please use the N-Queen problem (at least N=8 or more) or any simple perfect games. Please provide a screenshot of output and please heavily comment the code. Thanks!
Write pseudocode for quick find algorithm anf quick union algorithm Write pseudocode for quick find algorithm...
Write pseudocode for quick find algorithm anf quick union algorithm Write pseudocode for quick find algorithm and quick union algorithm
You will utilize a large dataset to create a predictive analytics algorithm in Python. For this...
You will utilize a large dataset to create a predictive analytics algorithm in Python. For this assignment, complete the following: Utilize one of the following Web sites to identify a dataset to use, preferably over 500K from Google databases, kaggle, or the .gov data website Utilize a machine learning algorithm to create a prediction. K-nearest neighbors is recommended as an introductory algorithm. Your algorithm should read in the dataset, segmenting the data with 70% used for training and 30% used...
Please find and share one sorting/searching algorithm. Explain it, and discuss its efficiency with your friends!
Please find and share one sorting/searching algorithm. Explain it, and discuss its efficiency with your friends!
(Lower bound for searching algorithms) Prove: any comparison-based searching algorithm on a set of n elements...
(Lower bound for searching algorithms) Prove: any comparison-based searching algorithm on a set of n elements takes time Ω(log n) in the worst case. (Hint: you may want to read Section 8.1 of the textbook for related terminologies and techniques.)
1) Please find and share one sorting/searching algorithm. Explain it, and discuss its efficiency 2) Please...
1) Please find and share one sorting/searching algorithm. Explain it, and discuss its efficiency 2) Please find and share algorithm about red/black trees. Explain it, give 2 real world usages and discuss its efficiency 3) Please find and share one algorithm about Binary Search Trees. Explain it, and discuss it's efficiency
Assume you need to write a Java program that uses a binary search algorithm to search...
Assume you need to write a Java program that uses a binary search algorithm to search a sorted array for a given value. 1. Write a Java pseudocode that uses recursion to accomplish the task. Here is a hint. When you are searching for a particular value in an array, there are two possible outcomes. 1) The value is found and the array index of that value is returned. 2) The value is not found and we return -1. (5...
I need to write a journal for my internship as an frontend web developer along with...
I need to write a journal for my internship as an frontend web developer along with few backend job. please help. it has to be 2 pages long and double spaced
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT