#include using namespace std; const size_t DATA_SIZE = 100; const int MAX_SIZE = 1000; const int TRIALS = 1000; const bool DEBUG = false; void FillArray(int data[], size_t size); void PrintArray(const int data[], size_t size); void MergeSort(int data[], size_t start, size_t end); void CopyArray(const int src[], int dest[], size_t size); bool BSearch(const int data[], size_t start, size_t end, int key); void CheckArray(const int data[], const int copy[] , size_t size); void CheckOrder(const int data[], size_t size); void MissCheck(const int data[], size_t size); int main() { int data[DATA_SIZE]; int copy[DATA_SIZE]; int trial; size_t size; for(size = 2; size <= DATA_SIZE; size++) { cout << "Testing Size " << size << ": "; for(trial = 1; trial <= TRIALS; trial ++) { if (DEBUG) { cout << "Trial " << trial << endl; } else { if (trial%100==0 ) { cout << trial << " "; } if (trial == TRIALS) { cout << endl; } } FillArray(data, size); CopyArray(data,copy, size); if (DEBUG) { cout << "The initial array " << endl; PrintArray(data, size); cout << endl; } MergeSort(data, 0, size); if (DEBUG) { cout << "The sorted array " << endl; PrintArray(data, size); cout << endl << endl; } CheckArray(data, copy, size); CheckOrder(data, size); MissCheck(data, size); } } return 0; } void FillArray(int data[], size_t size){ size_t i; for(i=0;i end) { return false; } mid = (start+end)/2; if (data[mid] == key) { return true; } else if (data[mid] > key and mid != start) { return BSearch(data, start, mid-1, key); } else { return BSearch(data, mid+1, end, key); } return false; } void CheckArray(const int data[], const int copy[] , size_t size){ size_t i; if (DEBUG) { cout << "Checking for missing elements " << endl; } for(i=0;i data[i]) { cout << "\tError: " << data[i-1] << " and " << data[i] << " are out of order " << endl; } } return; } void MissCheck(const int data[], size_t size){ if (BSearch(data, 0, size-1, -1) ) { cout << "\tError: found -1 in the array" << endl; } if (BSearch(data, MAX_SIZE+1, size-1, -1) ) { cout << "\tError: found " << MAX_SIZE+1 << " in the array" << endl; } }