// Nathanael Schwartz CS1A Chapter 8, P. 515, #8
//
/*******************************************************************************
*
* COUNT SEARCH COMPARISONS
* _____________________________________________________________________________
* This program stores at least 20 integers in an array, and allows the user to
* search for a value using both linear and binary search algorithms. The
* program counts the number of comparisons each search makes.
* _____________________________________________________________________________
* INPUT
* target : The value to search for in the array
*
* OUTPUT
* linearCount : Number of comparisons made by linear search
* binaryCount : Number of comparisons made by binary search
*
******************************************************************************/
#include <iostream>
using namespace std;
// Function prototypes
int linearSearch(const int[], int, int, int&);
int binarySearch(const int[], int, int, int&);
void selectionSort(int[], int);
int main() {
const int SIZE = 20;
int numbers[SIZE] = {35, 17, 48, 10, 26, 55, 13, 49, 18, 72,
29, 34, 65, 31, 12, 22, 50, 75, 61, 43};
int target, linearCount = 0, binaryCount = 0;
// Input: Prompt the user to enter the value to search for
cout << "Enter the value to search for: \n";
cin >> target;
// Perform linear search and display comparisons
int linearResult = linearSearch(numbers, SIZE, target, linearCount);
if (linearResult != -1) {
cout << "Linear Search: Value found at index " << linearResult
<< " with " << linearCount << " comparisons.\n";
} else {
cout << "Linear Search: Value not found after " << linearCount;
cout << " comparisons.\n";
}
// Sort the array for binary search
selectionSort(numbers, SIZE);
// Perform binary search and display comparisons
int binaryResult = binarySearch(numbers, SIZE, target, binaryCount);
if (binaryResult != -1) {
cout << "Binary Search: Value found at index " << binaryResult
<< " with " << binaryCount << " comparisons.\n";
} else {
cout << "Binary Search: Value not found after " << binaryCount;
cout << " comparisons.\n";
}
return 0;
}
/**************************************************************
* linearSearch *
* This function performs a linear search for a target value *
* in an array. It returns the index if found, or -1 if not. *
* It also counts the number of comparisons made. *
**************************************************************/
int linearSearch(const int array[], int size, int target, int &count) {
for (int i = 0; i < size; i++) {
count++; // Increment comparison count
if (array[i] == target) {
return i; // Target found
}
}
return -1; // Target not found
}
/**************************************************************
* binarySearch *
* This function performs a binary search for a target value *
* in a sorted array. It returns the index if found, or -1 if *
* not. It also counts the number of comparisons made. *
**************************************************************/
int binarySearch(const int array[], int size, int target, int &count) {
int low = 0;
int high = size - 1;
while (low <= high) {
int mid = (low + high) / 2;
count++; // Increment comparison count
if (array[mid] == target) {
return mid; // Target found
} else if (array[mid] < target) {
low = mid + 1; // Search right half
} else {
high = mid - 1; // Search left half
}
}
return -1; // Target not found
}
/**************************************************************
* selectionSort *
* This function sorts an integer array in ascending order *
* using the selection sort algorithm. *
**************************************************************/
void selectionSort(int array[], int size) {
for (int start = 0; start < size - 1; start++) {
int minIndex = start;
for (int index = start + 1; index < size; index++) {
if (array[index] < array[minIndex]) {
minIndex = index;
}
}
// Swap the found minimum element with the first element
int temp = array[minIndex];
array[minIndex] = array[start];
array[start] = temp;
}
}