I'm not sure what these instructions are asking of me

It's a binary search, bubble search, and sequential search program.

https://s32.postimg.org/arwpuzb91/image.png
Last edited on
You should write a program that searches a value in an array of integers in two different ways and then you should compare the efficiency of both search methods.

So:
- create an array with 20 numbers, lets say the numbers 1 to 20.
- ask the user what value you should search for.
- search the value using sequential search and store the number of steps that were required in a variable.
- search the value using binary search and store the number of steps that were required in another variable.

- run the program a number of times (for example 21 times) searching for a different value (1 to 21) every time (this could be a for-loop if you want to be fast).
- calculate the average number of steps required to find a number and formulate your conclusions.
What in the name of God.

Okay.
Here's a good video on binary search: https://www.youtube.com/watch?v=j5uXyPJ0Pew

Here's a video that does exactly what your assignment says: https://www.youtube.com/watch?v=r9OoXkKvQgQ
What a good day for the sound not to be working lol. I viewed it though, and I think it made sense.

I am trying to make progress though.

I can't get my array to display horizontally correctly and some of the output isn't fully showing on the screen for some reason.

How would you store the steps in a variable?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>

using namespace std;

const int SIZE=20;

void bubbleSort(int numbers[], int SIZE);


int main()
{
    int numbers[SIZE]= {26, 45, 56, 12, 78, 74, 39, 22, 5, 90, 87, 32, 28, 11, 93, 62, 79, 53, 22, 51};
    int value=0;
    bool found;

    cout << "Today we are going to be searching for values." << endl;
    cout << "These are the values you have to choose from" << endl;

    for (int i=0; i<SIZE; i++)
        cout << numbers[i]<<"; ";


    cout << "What value would you like to search for?" << endl;

    cin >> value;

    do
    {
        cout << "Make sure to enter a value that's in the list." << endl;
        cin >> value;
        found=false;
        for (int i=0; i<SIZE; i++)
        {
            if (value==numbers[i])
            {
                found=true;
                break;
            }
        }
        if (!found)
            cout << "Enter a valid value !" << endl;
    }
    while (!found);

    bubbleSort(numbers, SIZE);



    return 0;
}

void bubbleSort (int numbers[], int SIZE)
{
    int low=0;
    int high=SIZE-1;
    int mid;
    int count=0;
    int searchValue=0;

    while(low<=high)
    {
        mid=(low+high)/2;
        count++;

        if(searchValue==numbers[mid])
        {
            break;
        }
        else if (searchValue>numbers[mid])
        {
            low=mid+1;
        }
        else
        {
            high=mid-1;
        }
    }

    cout << "Binary search comparisons: " << cout << endl;

}


Will still be editing.
Last edited on
Topic archived. No new replies allowed.