#include #include using namespace std; const int MAX_VALUES{10}; const int DATA_LIMIT{40}; void FillArray(int data[], int & size); void SortArray(int data[], int size); void Swap(int & a ,int & b); void PrintArray(const int data[], int size); void SearchTest(const int data[], int size); int BinarySearch(const int data[], int size, int key); int LinearSearch(const int data[], int size, int key); void SearchMessage(const int data[], int size, int result, string msg); int main() { int data[MAX_VALUES]; int size; srand(static_cast(time(nullptr))); FillArray(data,size); PrintArray(data, size); SortArray(data,size); PrintArray(data, size); SearchTest(data, size); return 0; } int LinearSearch(const int data[], int size, int key){ bool found{false}; int foundPos{size}; int i{0}; while (i < size and not found) { if (data[i] == key) { found = true; foundPos = i; } else { i++; } } return foundPos; } void SearchMessage(const int data[], int size, int result , int key, string msg){ int pos; pos = BinarySearch(data, size, key); if ( pos != result ) { cout << "Error: " << msg << endl; cout << "Search returned " << pos << " not " << result << endl; } } void SearchTest(const int data[], int size){ int i; int mid; cout << endl; cout << "Running Binary Search Test " << endl; // strategy: first search for all of the data in the array for (i = 0; i < size; i++) { SearchMessage(data, size, i, data[i], " search failed, should find"); } // strategy: next search for data between each element for ( i = 0; i < size-1; i++) { mid = (data[i] + data[i+1] )/2; if (mid != data[i] and mid != data[i+1]) { SearchMessage(data, size, size, mid, " search failed, should not find"); } } // strategy: finally search for smaller than and larger than the array. SearchMessage(data, size, size, data[0]-10, "search failed"); SearchMessage(data, size, size, data[size-1]+10, "search failed"); cout << "Done running Binary Search Test " << endl; return; } void FillArray(int data[], int & size){ int i; int item; data[0] = rand() % DATA_LIMIT; for(i = 1; i < MAX_VALUES; ++i) { item = rand() % DATA_LIMIT; while(i != LinearSearch(data, i, item)) { item = rand() % DATA_LIMIT; } data[i] = item; } size = MAX_VALUES; return; } void PrintArray(const int data[], int size){ int i; for(i = 0; i < size; ++i) { cout << setw(5) << data[i]; } cout << endl; return; } void Swap(int & a ,int & b){ int tmp{a}; a = b; b = tmp; } void SortArray(int data[], int size){ int current, small, pos; for(current = 0; current < size; ++current) { small = current; for(pos = current+1; pos < size; ++pos) { if (data[pos] < data[small]) { small = pos; } } if (small != current) { Swap(data[small], data[current]); } } } int BinarySearch(const int data[], int size, int key){ int low{0}, high{size-1}; int mid; int returnValue{size}; bool found{false}; while ( low <= high and not found) { mid = (low+high)/2; if (data[mid] == key) { found = true; returnValue = mid; } else if (data[mid] < key) { low = mid+1; } else { high = mid -1; } } return returnValue; }