Question

The purpose of this assignment is to familiarize you with sort algorithms. Problem Description is as follows: 8. Search Benchmarks Write a program that has at least 20 250 integers stored in an ar...

The purpose of this assignment is to familiarize you with sort algorithms. Problem Description is as follows:

8. Search Benchmarks

Write a program that has at least 20 250 integers stored in an array in ascending order. It should call a function that uses the linear search algorithm to locate one of the values. The function should keep a count of the number of comparisons it makes until it finds the value. The program then should call a function that uses the binary search algorithm to locate the same value. It should also keep count of the number of comparisons it makes. Display these two counts on the screen.

bonus_assignment_INCOMPLETE.cpp

In this assignment, a 250 element array is created and populated with random numbers. Lines 27 and 28 place specific values in the array, which will be found since we placed them in the array. The program keeps a count of the number of comparisons made for linear searches and binary searches.

// Chapter 9 - Programming Challenge 8, Search Benchmarks
// This program compares the number of comparisons used by linear
// search vs. binary search to find a value.
#include <iostream>
#include <iomanip>
using namespace std;

// Function prototypes
int linearSearchBench(int[], int, int);
int binarySearchBench(int[], int, int);
void print_array(int *, int);
void sort_array(int *arr_param, int size);

int main()
{
        const int SIZE = 250;
        int numCompares;         // Accumulator to count the number of comparisons 

        int arr_tests[SIZE] = {};

        for (auto &one_num : arr_tests)
                one_num = rand() % 1001 + 2000;

        arr_tests[124] = 2496; // Place value in the middle of the array
        arr_tests[249] = 5000; //Place value at the end of the array

        sort_array(arr_tests, SIZE);
        print_array(arr_tests, SIZE);

        numCompares = linearSearchBench(arr_tests, SIZE, 2496);
        cout << "Linear Search. Searching for 2496 (middle of array). "
                        << "The linear search made " << numCompares
                        << " comparisons.\n";

        numCompares = linearSearchBench(arr_tests, SIZE, 5000);
        cout << "Linear Search. Searching for 5000 (last element in array). "
                        << " The linear search made " << numCompares
                        << " comparisons.\n";

        numCompares = linearSearchBench(arr_tests, SIZE, 8000);
        cout << "Linear Search. Searching for  8000 (Not in array). "
                        << "The linear search made " << numCompares
                << " comparisons.\n\n";

        numCompares = binarySearchBench(arr_tests, SIZE, 296);
        cout << "Binary Search. Searching for 2496. "
                        << "The binary search made " << numCompares
                        << " comparisons.\n";

        numCompares = binarySearchBench(arr_tests, SIZE, 5000);
        cout << "Binary Search. Searching for 5000 (last element in array). "
                        << "The binary search made " << numCompares
                        << " comparisons.\n";


        numCompares = binarySearchBench(arr_tests, SIZE, 8000);
        cout << "Binary Search. Searching for 8000 (Not in array). "
                        << "The binary search made " << numCompares
                << " comparisons.\n";

        system("pause");
        return 0;
}
/********************************************************************
 *                       print_array                                                            *
 * Called by: main                                                  *
 * Passed   : An array to sprint, the number of elements in                     *
 *            the array,                                                                        *
 * Purpose  : To print elements of the array                        *
 * Returns  : nothing                                                                                           *
 ********************************************************************/
void print_array(int *arr_param, int size)
{

}
/********************************************************************
 *                       sort_array                                                                     *
 * Called by: main                                                  *
 * Passed   : An array to sprint, the number of elements in                     *
 *            the array,                                                                        *
 * Purpose  : To use a sort algorithm and sort array        *
 * Returns  : nothing                                                                                           *
 ********************************************************************/
void sort_array(int *arr_param, int size)
{

}
/********************************************************************
 *                       linearSearchBench                          *
 * Called by: main                                                  *
 * Passed   : An array to search in, the number of elements in      *
 *            the array, and the value to search for                *
 * Purpose  : To count the number of comparisons made to complete   *
 *            the linear search                                     *
 * Returns  : The comparison count                                  *
 ********************************************************************/
int linearSearchBench(int arr_param[], int size, int inp_val)
{
        int  comparisons = 0;



        return comparisons;
}

/******************************************************************
 *                       binarySearchBench                        *
 * Called by: main                                                *
 * Passed   : An array to search in, the number of elements in    *
 *            the array, and the value to search for              *
 * Purpose  : To count the number of comparisons made to complete *
 *            the binary search                                   *
 * Returns  : The comparison count                                *
 ******************************************************************/
int binarySearchBench(int arr_param[], int size, int inp_val)
{
        bool found = false;
        int comparisons = 0;

        return comparisons;
}

Output from the completed version of the assignment is shown below:

2009 2016 2017 2017 2018 2027 2031 2032 2032 2037 2040 2041 2053 2053 2060 2070 2076 2076

2080 2084 2090 2092 2102 2109 2117 2126 2130 2137 2139 2149 2150 2152 2153 2155 2161 2168

2172 2175 2176 2177 2185 2192 2197 2218 2225 2244 2245 2258 2264 2266 2273 2273 2273 2275

2277 2280 2281 2282 2285 2288 2291 2292 2295 2296 2303 2304 2304 2312 2321 2323 2326 2326

2328 2329 2329 2335 2346 2353 2359 2364 2365 2369 2369 2370 2373 2392 2393 2397 2404 2407

2416 2419 2419 2427 2431 2434 2440 2441 2442 2442 2445 2449 2452 2460 2461 2465 2467 2467

2474 2477 2479 2491 2493 2494 2496 2496 2502 2503 2505 2517 2517 2520 2522 2525 2525 2527

2527 2532 2545 2558 2559 2559 2560 2568 2570 2570 2577 2580 2586 2590 2592 2597 2599 2599

2602 2609 2610 2611 2611 2612 2617 2618 2624 2626 2628 2629 2630 2632 2635 2642 2642 2649

2654 2656 2660 2670 2675 2682 2698 2699 2700 2703 2704 2705 2709 2715 2717 2723 2725 2725

2726 2730 2732 2738 2743 2744 2749 2751 2757 2776 2777 2778 2778 2782 2784 2785 2788 2790

2791 2796 2806 2811 2823 2824 2829 2829 2839 2841 2847 2862 2866 2868 2875 2876 2879 2885

2893 2894 2898 2899 2900 2901 2907 2911 2917 2918 2921 2922 2925 2928 2931 2931 2934 2935

2936 2951 2952 2953 2962 2963 2964 2975 2982 2990 2992 2993 2998 2998 2998 5000

Linear Search. Searching for 2496 (middle of array). The linear search made 115 comparisons.

Linear Search. Searching for 5000 (last element in array). The linear search made 250 comparisons.

Linear Search. Searching for 8000 (Not in array). The linear search made 250 comparisons.

Binary Search. Searching for 2496. The binary search made 8 comparisons.

Binary Search. Searching for 5000 (last element in array).The binary search made 8 comparisons.

Binary Search. Searching for 8000 (Not in array). The binary search made 8 comparisons.

Press any key to continue . . .

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// Chapter 9 - Programming Challenge 8, Search Benchmarks

// This program compares the number of comparisons used by linear

// search vs. binary search to find a value.

#include <iostream>

#include <iomanip>

#include <cstdlib>

using namespace std;

// Function prototypes

int linearSearchBench(int[], int, int);

int binarySearchBench(int[], int, int);

void print_array(int *, int);

void sort_array(int *arr_param, int size);

int main()

{

const int SIZE = 250;

int numCompares; // Accumulator to count the number of comparisons

int arr_tests[SIZE] = {};

for (auto &one_num : arr_tests)

one_num = rand() % 1001 + 2000;

arr_tests[124] = 2496; // Place value in the middle of the array

arr_tests[249] = 5000; //Place value at the end of the array

sort_array(arr_tests, SIZE);

print_array(arr_tests, SIZE);

numCompares = linearSearchBench(arr_tests, SIZE, 2496);

cout << "\n\nLinear Search. Searching for 2496 (middle of array). "

<< "The linear search made " << numCompares

<< " comparisons.\n";

numCompares = linearSearchBench(arr_tests, SIZE, 5000);

cout << "Linear Search. Searching for 5000 (last element in array). "

<< " The linear search made " << numCompares

<< " comparisons.\n";

numCompares = linearSearchBench(arr_tests, SIZE, 8000);

cout << "Linear Search. Searching for 8000 (Not in array). "

<< "The linear search made " << numCompares

<< " comparisons.\n\n";

numCompares = binarySearchBench(arr_tests, SIZE, 2496);

cout << "\nBinary Search. Searching for 2496. "

<< "The binary search made " << numCompares

<< " comparisons.\n";

numCompares = binarySearchBench(arr_tests, SIZE, 5000);

cout << "Binary Search. Searching for 5000 (last element in array). "

<< "The binary search made " << numCompares

<< " comparisons.\n";

numCompares = binarySearchBench(arr_tests, SIZE, 8000);

cout << "Binary Search. Searching for 8000 (Not in array). "

<< "The binary search made " << numCompares

<< " comparisons.\n";

system("pause");

return 0;

}// End of main function

/********************************************************************

* print_array *

* Called by: main *

* Passed : An array to sprint, the number of elements in *

* the array, *

* Purpose : To print elements of the array *

* Returns : nothing *

********************************************************************/

void print_array(int *arr_param, int size)

{

cout<<"\n ************ Array Contents ************ \n";

for(int c = 0; c < size; c++)

cout<<arr_param[c]<<", ";

cout<<endl;

}

/********************************************************************

* sort_array *

* Called by: main *

* Passed : An array to sprint, the number of elements in *

* the array, *

* Purpose : To use a sort algorithm and sort array *

* Returns : nothing *

********************************************************************/

void sort_array(int *arr_param, int size)

{

// Loops till number of elements

for(int r = 0; r < size; r++)

{

// Loops till number of elements minus outer loop value minus one

// Minus outer loop value because after each iteration one record is sorted

// Minus one because numbers are compared in pair

for(int c = 0; c < size - r - 1; c++)

{

// Checks if current element value is greater than the next element value

if(arr_param[c] > arr_param[c + 1])

{

// Swapping process

int temp = arr_param[c];

arr_param[c] = arr_param[c + 1];

arr_param[c + 1] = temp;

}// End of if condition

}// End of inner for loop

}// End of outer for loop

}// End of function

/********************************************************************

* linearSearchBench *

* Called by: main *

* Passed : An array to search in, the number of elements in *

* the array, and the value to search for *

* Purpose : To count the number of comparisons made to complete *

* the linear search *

* Returns : The comparison count *

********************************************************************/

int linearSearchBench(int arr_param[], int size, int inp_val)

{

// To store number of comparisons

int comparisons = 0;

// Loops till number of elements in the array

for(int c = 0; c < size; c++, comparisons++)

{

// Checks if current element of the array is equals to parameter inp_val value

if(arr_param[c] == inp_val)

// Returns number of comparisons

return ++comparisons;

}// End of for loop

// Otherwise not found, returns number of comparisons

return comparisons;

}// End of function

/******************************************************************

* binarySearchBench *

* Called by: main *

* Passed : An array to search in, the number of elements in *

* the array, and the value to search for *

* Purpose : To count the number of comparisons made to complete *

* the binary search *

* Returns : The comparison count *

******************************************************************/

int binarySearchBench(int arr_param[], int size, int inp_val)

{

static bool found = false;

static int comparisons = 0;

int first = 0, last = size - 1, middle;

// Loops till found status is not false or first element is less than last element

while (!found)

{

// Calculates the middle value

middle = (first + last) / 2;

// Checks if array mid element is equals to parameter value

if (arr_param[middle] == inp_val)

{

// Sets the found status to true

found = true;

// Increase the comparison counter by one

comparisons++;

// Returns number of comparisons

return ++comparisons;

}// End of if condition

// Otherwise checks if array mid element is greater than parameter value

else if (arr_param[middle] > inp_val)

{

// Updates the last position by middle position minus 1

last = middle - 1;

// Increase the comparison counter by one

comparisons++;

}// End of else if condition

// Otherwise array mid element is less than parameter value

else

{

// Updates the first position by middle position plus 1

first = middle + 1;

// Increase the comparison counter by one

comparisons++;

}// End of else

}// End of while loop

// Returns number of comparisons

return comparisons;

}// End of binary search

Sample Output:

************ Array Contents ************
2009, 2016, 2017, 2017, 2018, 2027, 2031, 2032, 2032, 2037, 2040, 2041, 2053, 2053, 2060, 2070, 2076, 2076, 2080, 2084, 2090, 2092, 2102, 2109, 2117, 2126, 2130, 2137, 2139, 2149, 2150, 2152, 2153, 2155, 2161, 2168, 2172, 2175, 2176, 2177, 2185, 2192, 2197, 2218, 2225, 2244, 2245, 2258, 2264, 2266, 2273, 2273, 2273, 2275, 2277, 2280, 2281, 2282, 2285, 2288, 2291, 2292, 2295, 2296, 2303, 2304, 2304, 2312, 2321, 2323, 2326, 2326, 2328, 2329, 2329, 2335, 2346, 2353, 2359, 2364, 2365, 2369, 2369, 2370, 2373, 2392, 2393, 2397, 2404, 2407, 2416, 2419, 2419, 2427, 2431, 2434, 2440, 2441, 2442, 2442, 2445, 2449, 2452, 2460, 2461, 2465, 2467, 2467, 2474, 2477, 2479, 2491, 2493, 2494, 2496, 2496, 2502, 2503, 2505, 2517, 2517, 2520, 2522, 2525, 2525, 2527, 2527, 2532, 2545, 2558, 2559, 2559, 2560, 2568, 2570, 2570, 2577, 2580, 2586, 2590, 2592, 2597, 2599, 2599, 2602, 2609, 2610, 2611, 2611, 2612, 2617, 2618, 2624, 2626, 2628, 2629, 2630, 2632, 2635, 2642, 2642, 2649, 2654, 2656, 2660, 2670, 2675, 2682, 2698, 2699, 2700, 2703, 2704, 2705, 2709, 2715, 2717, 2723, 2725, 2725, 2726, 2730, 2732, 2738, 2743, 2744, 2749, 2751, 2757, 2776, 2777, 2778, 2778, 2782, 2784, 2785, 2788, 2790, 2791, 2796, 2806, 2811, 2823, 2824, 2829, 2829, 2839, 2841, 2847, 2862, 2866, 2868, 2875, 2876, 2879, 2885, 2893, 2894, 2898, 2899, 2900, 2901, 2907, 2911, 2917, 2918, 2921, 2922, 2925, 2928, 2931, 2931, 2934, 2935, 2936, 2951, 2952, 2953, 2962, 2963, 2964, 2975, 2982, 2990, 2992, 2993, 2998, 2998, 2998, 5000,


Linear Search. Searching for 2496 (middle of array). The linear search made 115 comparisons.
Linear Search. Searching for 5000 (last element in array). The linear search made 250 comparisons.
Linear Search. Searching for 8000 (Not in array). The linear search made 250 comparisons.


Binary Search. Searching for 2496. The binary search made 8 comparisons.
Binary Search. Searching for 5000 (last element in array). The binary search made 8 comparisons.
Binary Search. Searching for 8000 (Not in array). The binary search made 8 comparisons.

Add a comment
Know the answer?
Add Answer to:
The purpose of this assignment is to familiarize you with sort algorithms. Problem Description is as follows: 8. Search Benchmarks Write a program that has at least 20 250 integers stored in an ar...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The purpose of this assignment is to familiarize you with sort algorithms. Problem Description is as follows: 8. Search Benchmarks Write a program that has at least 20 250 integers stored in an ar...

    The purpose of this assignment is to familiarize you with sort algorithms. Problem Description is as follows: 8. Search Benchmarks Write a program that has at least 20 250 integers stored in an array in ascending order. It should call a function that uses the linear search algorithm to locate one of the values. The function should keep a count of the number of comparisons it makes until it finds the value. The program then should call a function that...

  • C programming (you don't need to write program) Problem 1 [Linear Search with Early Stop] Below...

    C programming (you don't need to write program) Problem 1 [Linear Search with Early Stop] Below you will find a linear search function with early stop. A linear search is just a naive search - you go through each of the elements of a list one by one. Early stop works only on sorted list. Early stop means, instead of going through whole list, we will stop when your number to search can no longer be possibly found in the...

  • In C language Write a program that includes a function search() that finds the index of...

    In C language Write a program that includes a function search() that finds the index of the first element of an input array that contains the value specified. n is the size of the array. If no element of the array contains the value, then the function should return -1. The program takes an int array, the number of elements in the array, and the value that it searches for. The main function takes input, calls the search()function, and displays...

  • My following program has an array which holds 1000 random integers between 1-1000. Now I need...

    My following program has an array which holds 1000 random integers between 1-1000. Now I need help to create an array that holds 10,000 random integer between 1-1000 in my following program. The main goal of this program is time analysis by using bubble sort and binary search algorithms. Please do the following task; 1. Replace the 1000 random integers with 10,000 random integers After change please answer the following question 2. what will be happen, if an array holds...

  • Benchmark Searching and Sorting Write a program that has an array of at least 20 strings...

    Benchmark Searching and Sorting Write a program that has an array of at least 20 strings that you will have your user enter. It should call a function that uses the linear search algorithm to locate one of the values. The function should keep a count of the number of comparisons it makes until it finds the value. The program then should call a function that uses the binary search algorithm to locate the same value. It should also keep...

  • C++ Time the sequential search and the binary search methods several times each for randomly generated...

    C++ Time the sequential search and the binary search methods several times each for randomly generated values, then record the results in a table. Do not time individual searches, but groups of them. For example, time 100 searches together or 1,000 searches together. Compare the running times of these two search methods that are obtained during the experiment. Regarding the efficiency of both search methods, what conclusion can be reached from this experiment? Both the table and your conclusions should...

  • Programming Assignment #7 (Recursion) This assignment is to write some methods that perform simple array operations...

    Programming Assignment #7 (Recursion) This assignment is to write some methods that perform simple array operations recursively. Specifically, you will write the bodies for the recursive methods of the ArrayRecursion class, available on the class web page. No credit will be given if any changes are made to ArrayRecursion.java, other than completing the method bodies Note that the public methods of ArrayRecursion – contains(), getIndexOfSmallest(), and sort() – cannot be recursive because they have no parameters. Each of these methods...

  • I am currently using eclipse to write in java. A snapshot of the output would be...

    I am currently using eclipse to write in java. A snapshot of the output would be greatly appreciated to verify that the program is indeed working. Thanks in advance for both your time and effort. Here is the previous exercise code: /////////////////////////////////////////////////////Main /******************************************* * Week 5 lab - exercise 1 and exercise 2: * * ArrayList class with search algorithms * ********************************************/ import java.util.*; /** * Class to test sequential search, sorted search, and binary search algorithms * implemented in...

  • Practical 5: Write a program that implements several sorting algorithms, and use it to demonstrate the comparative perfo...

    Practical 5: Write a program that implements several sorting algorithms, and use it to demonstrate the comparative performance of the algorithms for a variety of data sets. Need Help With this Sorting Algorithm task for C++ Base Code for sorting.cpp is given. The header file is not included in this. Help would be much appreciated as I have not started on this due to personal reasons #include <cstdlib> #include <iostream> #include <getopt.h> using namespace std; long compares; // for counting...

  • Assignment 5 will be way different. It will be more like what you will receive in...

    Assignment 5 will be way different. It will be more like what you will receive in a programming shop. By that I mean that some things are built for you and others you will need to create or finish. P.S. part of your grade will include: Did you add in the required files? Did you rename your project? does your linear searches work? Did you put your code in the correct modules? did you change modules that you weren't supposed...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT