/* searching.cpp * * Array Searching program. Uses Binary and Linear search * methods to find values in an array. * * Bruce M. Bolden * November 7, 1997 * * http://www.cs.uidaho.edu/~bruceb/ * --------------------------------------------------------- */ #include #include #include #include #include // globals /* File streams are global to allow us to observe the * changes in the sorting routines. */ ifstream fIn; // Input file stream ofstream fOut; // Output file stream /* Uncomment these if TRUE and FALSE are not defined * by your compiler. */ const int TRUE = 1; const int FALSE = 0; // Prototypes /* Functions should be in separate files to promote * reuse of code. */ void OpenFiles( ifstream& fIn, char *inName, ofstream& fOut, char *outName ); void SearchRandomlyGeneratedArray(); int ReadArray( ifstream &fIn, int A[], const int nMax ); void WriteArray( ofstream &fOut, const int A[], const int nMax ); void WriteArrayRow( ofstream &fOut, const int A[], const int nMax ); void InitializeArray( int A[], const int nMax, const int iVal ); void CopyArray( int aFrom[], int aTo[], const int nMax ); int MinArray( const int A[], const int nMax ); int BinarySearch( const int A[], const int nMax, const int keyVal ); int LinearSearch( const int A[], const int nMax, const int keyVal ); void BubbleSort( int A[], const int nA ); void SelectionSort( int A[], const int nA ); int main() { const int MAX_ELEMENTS = 10; int iArray[MAX_ELEMENTS]; int iArray2[MAX_ELEMENTS]; int n; // number of elements in the array int index; // location of element in the array OpenFiles( fIn, "array.dat", fOut, "searching.out" ); n = ReadArray( fIn, iArray, MAX_ELEMENTS ); fOut << "Contents of iArray:\n"; WriteArrayRow( fOut, iArray, n ); // Save original array for later use CopyArray( iArray, iArray2, n ); fOut << "Contents of iArray2:\n"; WriteArrayRow( fOut, iArray2, n ); // Search, then Sort the array fOut << "\nLinear Searches" << endl; fOut << "_______________" << endl; fOut << "\nSearching for 1" << endl; index = LinearSearch( iArray, n, 1 ); fOut << "\tindex: " << index << endl; fOut << "\nCalling BubbleSort()\n"; BubbleSort( iArray, n ); fOut << "Array contents after Calling BubbleSort()\n"; WriteArrayRow( fOut, iArray, n ); fOut << "\nSearching for 1" << endl; index = LinearSearch( iArray, n, 1 ); fOut << "\tindex: " << index << endl; // Restore original array CopyArray( iArray2, iArray, n ); // Search, then Sort the array fOut << "\n\nBinary Searches" << endl; fOut << "_______________" << endl; fOut << "\nSearching for 1" << endl; index = BinarySearch( iArray, n, 1 ); fOut << "\tindex: " << index << endl; fOut << "\nCalling BubbleSort()\n"; BubbleSort( iArray, n ); fOut << "Array contents after Calling BubbleSort()\n"; WriteArrayRow( fOut, iArray, n ); fOut << "\nSearching for 1" << endl; index = BinarySearch( iArray, n, 1 ); fOut << "\tindex: " << index << endl; fOut << "\nSearching for 7" << endl; index = BinarySearch( iArray, n, 7 ); fOut << "\tindex: " << index << endl; // Search a bigger array SearchRandomlyGeneratedArray(); return 0; } /* OpenFiles * * Open input and output file (streams). */ void OpenFiles( ifstream& fIn, char *inName, ofstream& fOut, char *outName ) { fIn.open( inName, ios::in ); if( !fIn ) { cerr << "Unable to open input file: \"" << inName << "\"" << endl; exit( -1 ); } fOut.open( outName, ios::out ); if( !fOut ) { cerr << "Unable to open output file: \"" << outName << "\"" << endl; exit( -1 ); } } /* SearchRandomlyGeneratedArray * * Search a randomly generated array. */ void SearchRandomlyGeneratedArray() { const int A_SIZE = 100; int B[A_SIZE]; int index; fOut << "\n\nSearch a randomly generated array" << endl; fOut << "_________________________________\n" << endl; fOut << "Note the difference between the number of passes" << endl; fOut << "and the index returned by the Linear Search routine." << endl; // Generate array contents---could be a function for( int i = 0 ; i < A_SIZE ; ++i ) { B[i] = rand() % (A_SIZE+1); //cout << B[i] << endl; } // Sort the randomly generated array BubbleSort( B, A_SIZE ); int searchVals[5] = {11, 25, 50, 74, 93}; for( int i = 0 ; i < 5 ; i++ ) { fOut << "\nSearching for " << searchVals[i] << endl; index = BinarySearch( B, A_SIZE, searchVals[i] ); fOut << "\tindex: " << index << endl; index = LinearSearch( B, A_SIZE, searchVals[i] ); fOut << "Linear search:" << endl; fOut << "\tindex: " << index << endl; } } // ------------------------------------------------------------------ /* ReadArray * * Reads array elements. */ int ReadArray( ifstream& fIn, int A[], const int nMax ) { int i = 0; int nTmp; while( (i < nMax) && (fIn >> nTmp) ) { A[i] = nTmp; ++i; } return i; // number of elements stored } /* WriteArray * * Writes one element to a line. */ void WriteArray( ofstream &fOut, const int A[], const int nMax ) { for( int i = 0 ; i < nMax ; i++ ) { fOut << A[i] << endl; } } /* WriteArrayRow * * Writes all elements on a line. */ void WriteArrayRow( ofstream &fOut, const int A[], const int nMax ) { for( int i = 0 ; i < nMax ; i++ ) { fOut << setw(4) << A[i]; } fOut << endl; } /* InitializeArray * * Initializes array elements to a programmer defined value. */ void InitializeArray( int A[], const int nMax, const int iVal ) { for( int i = 0 ; i < nMax ; i++ ) A[i] = iVal; } /* CopyArray * * Copies array elements from one array to another. */ void CopyArray( int aFrom[], int aTo[], const int nMax ) { for( int i = 0 ; i < nMax ; i++ ) aTo[i] = aFrom[i]; } /* MinArray * * Finds the minimum value in an array. */ int MinArray( const int A[], const int nMax ) { int min = A[0]; for( int i = 1 ; i < nMax ; i++ ) { if( A[i] < min ) min = A[i]; } return min; } // ------------------------------------------------------------------ /* BinarySearch * * Searches for the value, keyVal, in a sorted array * (values must be low to high). * If successful, the index is returned. * If unsuccessful, -1 is returned. */ int BinarySearch( const int A[], const int nMax, const int keyVal ) { int first = 0; int last = nMax-1; int middle; int pass = 1; fOut << "\nIn BinarySearch():" << endl; if( (keyVal < A[first]) || (keyVal > A[last]) ) return -1; while( first <= last ) { fOut << " Pass: " << pass++ << endl; middle = int((first+last)/2 - (first+last)%2); if( keyVal == A[middle] ) return middle; else if( keyVal > A[middle] ) first = middle + 1; else // keyval is less than A[middle] last = middle - 1; } return -1; } /* LinearSearch * * Searches for the value, keyVal, in an array. * If successful, the index is returned. * If unsuccessful, -1 is returned. */ int LinearSearch( const int A[], const int nMax, const int keyVal ) { for( int i = 0 ; i < nMax ; i++ ) { if( A[i] == keyVal ) return i; } return -1; } // ------------------------------------------------------------------ /* BubbleSort * * Perform a buble sort on an array. */ void BubbleSort( int A[], const int nA ) { int iTmp; int swapFlag = TRUE; //fOut << "\nIn BubbleSort():" << endl; //fOut << "Pass Array Contents" << endl; for( int i = 0 ; i < nA && swapFlag == TRUE ; i++ ) { swapFlag = FALSE; for( int j = 0 ; j < nA - i - 1 ; j++ ) { if( A[j] > A[j+1] ) // swap them { swapFlag = TRUE; //Swap( A[j], A[j+1] ); iTmp = A[j]; A[j] = A[j+1]; A[j+1] = iTmp; } } //fOut << setw(3) << i+1; // pass number //WriteArrayRow( fOut, A, nA ); } //fOut << "Leaving BubbleSort()\n" << endl; } /* SelectionSort * * Perform a selection sort on an array */ void SelectionSort( int A[], const int nA ) { fOut << "\nIn SelectionSort():" << endl; fOut << "Pass Array Contents" << endl; for( int i = 0 ; i < nA - 1 ; ++i ) { int low = i; for( int j = i + 1 ; j < nA ; ++j ) { if( A[j] < A[low] ) low = j; } if( i != low ) { int iTmp; //Swap( A[low], A[i] ); iTmp = A[low]; A[low] = A[i]; A[i] = iTmp; } fOut << setw(3) << i+1; // pass number WriteArrayRow( fOut, A, nA ); } fOut << "Leaving SelectionSort()\n" << endl; } // ------------------------------------------------------------------