So I made the following program which shows the difference between linear and binary search, in terms of the number of checks each takes to find the search value in the array.
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 82 83 84 85 86 87 88
|
#include <iostream>
using namespace std;
// Prototype functions
void FillArray(int[], int);
int LinearSearch(int[], int, int);
int BinarySearch (int[], int, int);
int main()
{
//sets array size;
int ARRAY_SIZE;
cout << "Enter in size of array: " << flush;
cin >> ARRAY_SIZE;
int arr[ARRAY_SIZE];
int searchValue;
cout << "Enter in a number between 0 and " << ARRAY_SIZE << " to search for: " << flush;
cin >> searchValue;
cout << "\n\n";
// Function call to fill array
FillArray(arr, ARRAY_SIZE);
//Calls LinearSearch
LinearSearch(arr, ARRAY_SIZE, searchValue);
// Calls BinarySearch function
BinarySearch(arr, ARRAY_SIZE, searchValue);
return 0;
}
void FillArray(int arr[], int elements)
{
// Fills array from 1 to number of elements
for(int i=1; i<=elements; i++)
arr[i-1] = i;
}
int LinearSearch(int arr[], int elements, int searchValue)
{
for(int i=0; i<elements; i++)
if(arr[i]==searchValue)
cout << "Number of checks taken for Linear Search: " << i+1 << endl;
// Number of checks is one more than i, since i starts with 0;
// if you want to know the index, return i.
}
int BinarySearch (int arr[], int elements, int searchValue)
{
int low = 0;
int high = elements-1;
int mid;
int checks = 0; // counts number of checks taken
// keep looping until low and high cross-over
while(low <= high)
{
// makes new mid with each loop as high and low change
mid = (low + high)/2;
checks++; // increments with each loop
if(searchValue > arr[mid])
low = mid + 1;
else if(searchValue < arr[mid])
high = mid - 1;
else if (searchValue == arr[mid])
break;
}
cout << "Number of checks taken for Binary Search: " << checks << endl;
}
|
As you can see, I fill in the array starting from 1.
Now if I enter in 5000 for the array size, and 4785 as the value im searching for, I get the following results:
Enter in size of array: 5000
Enter in a number between 0 and 5000 to search for: 4785
Number of checks taken for Linear Search: 4785
Number of checks taken for Binary Search: 8
But if I change the fill array function so the values in the array start from 0, like this:
1 2 3 4 5 6
|
void FillArray(int arr[], int elements)
{
// Fills array from 0 to number of elements-1
for(int i=0; i<elements; i++)
arr[i] = i;
}
|
This now gives me a completely different result for binary search:
Enter in size of array: 5000
Enter in a number between 0 and 5000 to search for: 4785
Number of checks taken for Linear Search: 4786
Number of checks taken for Binary Search: 12
Binary checks jumped from 8 to 12? How is that possible. All I did was change the array so it had values from 0-4999 instead of 1-5000. I expected the increase of one in the linear search, but I dont understand how the binary search increased by 4.
Any ideas?