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 89 90 91 92 93 94 95 96 97
|
#include <stdlib.h>
#include <iostream>
#include <fstream>
using namespace std;
bool LinearSearch(int A[], int n, int v){
bool found = false;
int work = 0;
// Open the file and create a stream for writing
ofstream out;
out.open("linear++.csv", ios::app);
// Start at the beginning of the array and search for value
for (int s = 0; s < n; s++){
work++; // increment for the comparison (s < n)
if (v == A[s]){
work++; // increment for the comparison (v == A[s])
found = true;
cout << "Success! It took " << work << " work to find.\n";
out << v << "," << work << "," << n << "\n";
out.close();
return found;
}
work++; // for incrementing the loop
}
return found;
}
bool BinarySearch(int A[], int n, int v){
int first = 0; // keeps the index for the start of the search range
int upto = n; // keeps the index for the end of the search range
int work = 0; // keeps track of comparisons, increments, and additions
bool found = false;
// Open the file and create a stream for writing
ofstream out;
out.open("binary++.csv", ios::app);
// Binary Algorithm
while (first < upto){
work++; // for the comparison (first < upto)
int mid = (first + upto)/2; // The midpoint of the array
work++; // Calculation of mid
if (v < A[mid]){
work++; // for the comparison in the if statement
upto = mid;
} else if (v > A[mid]){
work++; // for the comparison in the else if statement
first = mid + 1;
} else {
found = true; // We found our number
cout << "Success! It took " << work << " work to find\n";
out << v << "," << work << "," << n << "\n";
out.close();
return found;
}
}
return found;
}
int main() {
// Create a file for the results
ofstream lf ("linear++.csv");
ofstream bf ("binary++.csv");
// Create a header row for easy import to spreadsheet
lf << "Value, Work, Size \n";
bf << "Value, Work, Size \n";
// Close the files
lf.close();
bf.close();
cout << "Starting Search Comparison Program\n";
// Set the array lengths in an array
int N [] = {10, 100, 500, 1000, 5000, 10000, 50000, 100000, 500000, 1000000};
cout << "N Values Loaded\n";
// Loop 10 times, once for each integer in N
for (int i = 0; i < 10; i++){
// test array is the array that will be passed into the functions
int test [N[i]];
cout << "Intializing the test array\n";
// Loop through the test array and create an ordered list
for (int j = 0; j < N[i]; j++){
test[j] = j;
}
cout << "Array initialized.\n";
// Run the test of each search of N integers 100 times
for (int l = 0; l < 100; l++){
int value = (rand()%N[i]);
cout << "The value I'm searching for is " << value << " \n\n";
cout << "Starting the Linear Search Function\n";
LinearSearch(test, N[i], value);
cout << "Finished Linear Search\n";
cout << "Starting Binary Search\n";
BinarySearch(test, N[i], value);
cout << "Finished Binary Search\n";
}
}
return (EXIT_SUCCESS);
}
|