//--------------------------------------------------------------------------- // // An program that demonstrates different ways of searching an array of // numbers. // // Simon Parsons // November 26th 2008. // //--------------------------------------------------------------------------- // Include C++ library definitions #include // For input and output #include // For file handling using namespace std; // Set SIZE, which will hold the length of the array, and define LARGE // (which we will use in the auxilliary) to have the largest possible // integer value. #define SIZE 35 #define LARGE INT_MAX //--------------------------------------------------------------------------- // Function headers // linear search and binary search bool linearSearch(int want, int a[], int size); bool binarySearch(int want, int a[], int size); // To use binary search we need to sort the array first, and we'll use // bubble sort for that. void bubbleSort(int a[], int size); // To bubble sort we need a swap function. void swap(int &a, int &b); // A function that provides the user with a menu of different // ways to search the array int askSearch(void); // Print the array (useful for debugging) void printArray(int a[], int size); //--------------------------------------------------------------------------- // main int main() { // Variable definition and initialisation int numbers[SIZE]; int aux[SIZE]; int counter = 0; int choice = 0; int target; bool found; // Open file ifstream myfile; myfile.open("lots-of-numbers.txt"); // Read in numbers as we have up to now, assuming that there are // enough. while(counter < SIZE && !myfile.eof()) { myfile >> numbers[counter]; counter++; } // Initialise aux to have lots of large values in it in case we need to // sort the array. for(counter = 0; counter < SIZE; ++counter){ aux[counter] = LARGE; } // Ask user to select which search to use: choice = askSearch(); cout << endl << "What number are you looking for? "; cin >> target; cout << endl; // Use a switch statement to select the right way to sort. switch(choice){ case 0: found = linearSearch(target, numbers, SIZE); break; case 1: bubbleSort(numbers, SIZE); found = binarySearch(target, numbers, SIZE); break; default: // don't search the array ; } if(found){ cout << "We have it" << endl; } else{ cout << "The number isn't there!" << endl; } return 0; } //--------------------------------------------------------------------------- // // Function definitions // linearSearch // // Look through the array a until we find want. If we do find it, then // return truw. If we got to the end of the loop, then want is not in the array// and we return false. bool linearSearch(int want, int a[], int size) { int i; cout << "Checking: "; for(i = 0; i < size; ++i){ cout << a[i] << " "; if(a[i] == want){ cout << endl; return true; } } cout << endl; return false; } // binarySearch // // Compare want with the value in the middle of the array. If it is // the value in the middle, we have found what we want. If want is // bigger, repeat the search on the half of the array with larger // numbers in it. If want is smaller, repeat the search on the half of // the array with smaller numbers in it. // // Based on an example by Fred Swartz bool binarySearch(int want, int a[], int size) { int start = 0; // start and stop are the initial and final indexes of the int stop = size; // bit of the array we are looking in. Their initial values // are the limits of the array. int mid; // the index of the middle (or so) of the bit of the array // we are looking in. cout << "Checking: "; while(start <= stop){ mid = (start + stop)/2; cout << a[mid] << " "; if(want == a[mid]){ cout << endl; return true; } if(want > a[mid]){ start = mid + 1; } if(want < a[mid]){ stop = mid - 1; } } // If we get here, the while loop didn't find what we are looking // for, and want is not in a. cout << endl; return false; } // swap // // Just like it was when we saw it before. void swap(int &a, int &b) { int tmp; tmp = a; a = b; b = tmp; } // bubbleSort // // bubble sort repeat the j loop (using the i loop) to take care of // the case in which the smallest number is at the end. void bubbleSort(int a[], int size) { int i, j; // Repeat as many times as there are elements in the array. for(i = 0; i < size; ++i) { // For each position, compare that number with the one that // follows. for(j = 0; j < (size - 1); j++) { // If the following number is smaller, swap the two. if(a[j] > a[j+1]) { swap(a[j], a[j+1]); } } } } // askSearch // // Offer the user a menu of searches. Loop until the user makes a // sensible choice. int askSearch(void) { int choose = -1; while(choose < 0 || choose > 2){ cout << endl << endl; cout << "Choose your search:" << endl; cout << "\t 0: linear search \n"; cout << "\t 1: binary search \n"; cout << "\t 2: neither \n\n"; cout << "Your choice => "; cin >> choose; } return choose; }