Sessions 26 and 27 - Data Structures &amp; Algorithm

# Element 39 40 print remaining elements of vector to be

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 5 using std::endl; 6 #include <cstdlib> // prototypes for functions srand and rand int binarySearch( int ) const; 7 void displayElements() const; 8 using std::rand; private: 9 using std::srand; int size; 10 11 #include <ctime> // prototype for function time vector< int > data; 12 using std::time; void displaySubElements( int, int ) const; 13 }; 14 #include <algorithm> // prototype for sort function 15 #include "BinarySearch.h" // class BinarySearch definition 16 17 // constructor initializes vector with random ints and sorts the vector 18 BinarySearch::BinarySearch( int vectorSize ) 19 { 20 size = ( vectorSize > 0 ? vectorSize : 10 ); // validate vectorSize 21 srand( time( 0 ) ); // seed using current time 22 23 // fill vector with random ints in range 10-99 24 for ( int i = 0; i < size; i++ ) 25 data.push_back( 10 + rand() % 90 ); // 10-99 26 27 std::sort( data.begin(), data.end() ); // sort the data 28 } // end BinarySearch constructor 29 Sharif University of Technology Introduction to Programming: Sessions 26 and 27: Data Structures & Algorithm 86 30 // perform a binary search on the data 31 int BinarySearch::binarySearch( int searchElement ) const 32 { 33 int low = 0; // low end of the search area 34 int high = size - 1; // high end of the search area 35 int middle = ( low + high + 1 ) / 2; // middle element 36 int location = -1; // return value; -1 if not found 37 38 do // loop to search for element 39 { 40 // print remaining elements of vector to be searched 41 displaySubElements( low, high ); 42 43 // output spaces for alignment 44 for ( int i = 0; i < middle; i++ ) 45 cout << " "; 46 47 cout << " * " << endl; // indicate current middle 48 48 49 // if the element is found at the middle 50 if ( searchElement == data[ middle ] ) 51 location = middle; // location is the current middle 52 else if ( searchElement < data[ middle ] ) // middle is too high 53 high = middle - 1; // eliminate the higher half 54 else // middle element is too low 55 low = middle + 1; // eliminate the lower half 56 57 middle = ( low + high + 1 ) / 2; // recalculate the middle 58 } while ( ( low <= high ) && ( location == -1 ) ); 59 60 return location; // return location of search key 61 } // end function binarySearch Sharif University of Technology Introduction to Programming: Sessions 26 and 27: Data Structures & Algorithm 87 62 63 // display values in vector 64 void BinarySearch::displayElements() const 65 { 66 displaySubElements( 0, size - 1 ); 67 } // end function displayElements 68 69 // display certain values in vector 70 void BinarySearch::displaySubElements( int low, int high ) const 71 { 72 for ( int i = 0; i < low; i++ ) // output spaces for alignment 73 cout << " "; 74 75 for ( int i = low; i <= high; i++ ) // output elements left in vector 76 cout << data[ i ] << " "; 77 78 cout << endl; 79 } // end function displaySubElements Sharif University of Technology Introduction to Programming: Sessions 26 and 27: Data Structures & Algorithm 88 1 2 // BinarySearch test program. 3 #include <iostream> 4 using std::cin; 5 using std::cout; 6 using std::endl; 7 8...
View Full Document

## This note was uploaded on 09/01/2013 for the course CE CE230 taught by Professor Arash during the Fall '13 term at Sharif University of Technology.

Ask a homework question - tutors are online