//CS 2308 Searching and Sorting #include #include using namespace std; //funtion prototypes int binary_search(const int a[ ], int key, int low, int high); void swap (int &num1, int &num2); void bubbleSort1 (int list[], const int numElements); void bubbleSort2 (int list[], const int numElements); void bubbleSort3 (int list[], const int numElements); void selectionSort (int list[], const int numElements); void showList(const int list[], const int numElements); //******************************** const int MAX_ELEMENTS = 25; int main () { int list[MAX_ELEMENTS]; int numElements; ifstream inputFile; inputFile.open("search.txt"); //check for file opening error if (!inputFile) { cout << "File not found" << endl; system("PAUSE"); return -1; } numElements = 0; int num; inputFile >> num; while ((num != -1) && (numElements < MAX_ELEMENTS)) { list[numElements] = num; numElements++; inputFile >> num; } showList(list, numElements); cout << "\n\n"; int target = 47; int position = binary_search(list, target, 0, numElements - 1); if (position != -1) { cout << "FOUND target at postion- " << position << endl; } else cout << "target not found - " << target << endl; //bubbleSort1(list, numElements); //bubbleSort2(list, numElements); //bubbleSort3(list, numElements); //selectionSort(list, numElements); //showList(list, numElements); //cout << "\n\n"; system("PAUSE"); return 0; } //print the list in order to standard output void showList(const int list[], const int numElements) { for (int i = 0; i < numElements; i++) { cout << i << " - " << list[i] << endl; } } // binary_search searches for a key value in a sorted array // and returns true if the key value is found in the array // or false if the key value is not found in the array // receives: // a - array of ints sorted in ascending order // key - value to be searched for // high - index of highest occupied cell of the array // low - index of lowest occupied cell of the array // returns: the index of the key if found, otherwise returns -1 int binary_search(const int a[ ], int key, int low, int high) { return 0; } // swap // swaps the values in the parameters void swap (int &num1, int &num2) { int temp = num1; num1 = num2; num2 = temp; return; } //bubbleSort //version 1 //sorts the list into ascending order void bubbleSort1 (int list[], const int numElements) { int compares = 0; int swaps = 0; cout << "compares = " << compares << endl; cout << "swaps = " << swaps << endl; } //bubbleSort //version 2 //sorts the list into ascending order void bubbleSort2 (int list[], const int numElements) { int compares = 0; int swaps = 0; cout << "compares = " << compares << endl; cout << "swaps = " << swaps << endl; } //bubbleSort //version 3 //sorts the list into ascending order void bubbleSort3 (int list[], const int numElements) { int compares = 0; int swaps = 0; cout << "compares = " << compares << endl; cout << "swaps = " << swaps << endl; } //selection sort //sorts list into ascending order void selectionSort (int list[], const int numElements) { int compares = 0; int swaps = 0; cout << "compares = " << compares << endl; cout << "swaps = " << swaps << endl; }