Question

In: Computer Science

C++Design and implement a program (name it LinearBinarySearch) to implement and test the linear and binary...

C++Design and implement a program (name it LinearBinarySearch) to implement and test the linear and binary search algorithm discussed in the lecture slides. Define method LinearSearch() to implement linear search of an array of integers. Modify the algorithm implementation to count number of comparisons it takes to find a target value (if exist) in the array. Define method BinarySearch() to implement binary search of an array of integers. Modify the algorithm implementation to count number of comparisons it takes to find a target value (if exist) in the array. Now, develop a test method to read integer values from the user in to an array and then call methods LinearSearch() and BinarySearch() and printout the number of comparison took to find the target values using each search method. Document your code and organized your outputs as follows: Arrays Values: Target value: Linear Search Comparisons: Binary Search Comparisons:

Solutions

Expert Solution

#include <iostream>

using namespace std;
#define SIZE 20
// performs linear search on the array and returns number of comparisons
   // required
   int linearSearch(int aA[], int aEle) {
       for (int i = 0; i < SIZE; i++) {
           if (aA[i] == aEle)
               return i + 1;
       }
       return -1;
   }
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) {
           comp++;
           int mid = l + (r - l) / 2;
           if (arr[mid] == x) {
               return comp;
           }
           if (arr[mid] < x)
               return binarySearch(arr, mid+ 1, r, x, comp);

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

   int main() {
       int *a= new int[SIZE];
       int *b = new int[SIZE];
       for (int i = 0; i < SIZE; ++i) {
           a[i] = rand()%1000;
           // copying values into array
           b[i] = a[i];
       }
       bubbleSort(a,SIZE);
       // printing array
       for (int i = 0; i < SIZE; ++i) {
           cout<<b[i] << " ";
       }
       // reading element to search in the array
       cout<<"\nEnter element to search: ";
       int ele;
       cin>>ele;
       // calling binary and linear search
       cout<<binarySearch(a, 0, SIZE, ele, 0) << " comparisions required to find " << ele
               << " using binary search\n";
       cout<<linearSearch(b, ele) << " comparisions required to find " << ele << " using linear search\n";

   }

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


Related Solutions

IN C++ PLEASE!!! Design and implement a program (name it Coins) that determines the values of...
IN C++ PLEASE!!! Design and implement a program (name it Coins) that determines the values of coins in a jar. The program prints out the total dollars and cents in the jar. The program prompts the user to enter the number of coins (quarters, dimes, nickels, and pennies). Print out the number of coins entered for each coin type on separate lines followed by the total amount of money in the jar as dollars and cents as shown below.
IN C++ LANGUAGE PLEASE::: Design and implement a program (name it Rectangle) to calculate and display...
IN C++ LANGUAGE PLEASE::: Design and implement a program (name it Rectangle) to calculate and display the area and perimeter of a rectangle with width = 4 and height = 8.
DO THIS IN C#. Design and implement a program (name it MinMaxAvg) that defines three methods...
DO THIS IN C#. Design and implement a program (name it MinMaxAvg) that defines three methods as follows: Method max (int x, int y, int z) returns the maximum value of three integer values. Method min (int X, int y, int z) returns the minimum value of three integer values. Method average (int x, int y, int z) returns the average of three integer values. In the main method, test all three methods with different input value read from the...
In C++ Design and implement a program (name it ProcessGrades) that reads from the user four...
In C++ Design and implement a program (name it ProcessGrades) that reads from the user four integer values between 0 and 100, representing grades. The program then, on separate lines, prints out the entered grades followed by the highest grade, lowest grade, and averages of all four grades. Format the outputs following the sample runs below.
USING C# Design and implement a program (name it Coins) that determines the values of coins...
USING C# Design and implement a program (name it Coins) that determines the values of coins in a jar. The program prints out the total dollars and cents in the jar. The program prompts the user to enter the number of coins (quarters, dimes, nickels, and pennies). Print out the number of coins entered for each coin type on separate lines followed by the total amount of money in the jar as dollars and cents as shown below. Sample run...
Using C# Design and implement a program (name it CheckPoint) that prompts the user to enter...
Using C# Design and implement a program (name it CheckPoint) that prompts the user to enter the x-coordinate then y-coordinate of a point (in a Cartesian plane) as integer values. The program prints out the entered values followed by the location of the point on the plane. The possibilities for a point are: the origin point, on the x-axis, on the y-axis, in the first quadrant, in the second quadrant, in the third quadrant, or in the fourth quadrant. Format...
**** In C++ ****Exercise #3: Design and implement a program (name it ArrayMethods), that defines 4...
**** In C++ ****Exercise #3: Design and implement a program (name it ArrayMethods), that defines 4 methods as follows: int arrayMax(int[] arr) returns the maximum value in the an array int arrayMin(int[] arr) returns the minimum value in an array void arraySquared(int[] arr) changes every value in the array to its square (value²) void arrayReverse(int[] arr) reverses the array (for example: array storing 7 8 9 becomes 9 8 7 ) The program main method creates a single-dimensional array of...
Using C# Design and implement a program (name it ProcessGrades) that reads from the user four...
Using C# Design and implement a program (name it ProcessGrades) that reads from the user four integer values between 0 and 100, representing grades. The program then, on separate lines, prints out the entered grades followed by the highest grade, lowest grade, and averages of all four grades. Format the outputs following the sample runs below. Sample run 1: You entered:   95, 80, 100, 70 Highest grade:100 Lowest grade:  70 Average grade:86.25
C++ Design and implement a program (name it ComputeAreas) that defines four methods as follows: Method...
C++ Design and implement a program (name it ComputeAreas) that defines four methods as follows: Method squareArea (double side) returns the area of a square. Method rectangleArea (double width, double length) returns the area of a rectangle. Method circleArea (double radius) returns the area of a circle. Method triangleArea (double base, double height) returns the area of a triangle. In the main method, test all methods with different input value read from the user. Document your code and properly label...
USING C# Design and implement a program (name it SumValue) that reads three integers (say X,...
USING C# Design and implement a program (name it SumValue) that reads three integers (say X, Y, and Z) and prints out their values on separate lines with proper labels, followed by their average with proper label. Comment your code properly.Format the outputs following the sample runs below. Sample run 1: X = 7 Y = 8 Z = 10 Average = 8.333333333333334
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT