Sessions 26 and 27 - Data Structures & Algorithm

Please enter an integer value 1 to quit 91 26 31 33

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

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: #include "BinarySearch.h" // class BinarySearch definition 9 10 int main() 11 { 12 int searchInt; // search key 13 int position; // location of search key in vector 14 15 // create vector and output it 16 BinarySearch searchVector ( 15 ); 17 searchVector.displayElements(); 18 19 // get input from user 20 cout << "\nPlease enter an integer value (-1 to quit): "; 21 cin >> searchInt; // read an int from user 22 cout << endl; 23 24 // repeatedly input an integer; -1 terminates the program Sharif University of Technology Introduction to Programming: Sessions 26 and 27: Data Structures & Algorithm 89 24 // repeatedly input an integer; -1 terminates the program 25 while ( searchInt != -1 ) 26 { 27 // use binary search to try to find integer 28 position = searchVector.binarySearch( searchInt ); 29 30 // return value of -1 indicates integer was not found 31 if ( position == -1 ) 32 cout << "The integer " << searchInt << " was not found.\n"; 33 else 34 cout << "The integer " << searchInt 35 << " was found in position " << position << ".\n"; 36 37 // get input from user 38 cout << "\n\nPlease enter an integer value (-1 to quit): "; 39 cin >> searchInt; // read an int from user 40 cout << endl; 41 } // end while 42 43 return 0; 44 } // end main Sharif University of Technology Introduction to Programming: Sessions 26 and 27: Data Structures & Algorithm 90 26 31 33 38 47 49 49 67 73 74 82 89 90 91 95 Please enter an integer value (-1 to quit): 38 26 31 33 38 47 49 49 67 73 74 82 89 90 91 95 * 26 31 33 38 47 49 49 * The integer 38 was found in position 3. Please enter an integer value (-1 to quit): 91 26 31 33 38 47 49 49 67 73 74 82 89 90 91 95 * 73 74 82 89 90 91 95 * 90 91 95 * The integer 91 was found in position 13. Sharif University of Technology Introduction to Programming: Sessions 26 and 27: Data Structures & Algorithm 91 Please enter an integer value (-1 to quit): 25 26 31 33 38 47 49 49 67 73 74 82 89 90 91 95 * 26 31 33 38 47 49 49 * 26 31 33 * 26 * The integer 25 was not found. Please enter an integer value (-1 to quit): -1 Sharif University of Technology Introduction to Programming: Sessions 26 and 27: Data Structures & Algorithm 92 Efficiency of Binary Search Worst-case scenario Searching a sorted vector of 1023 elements will take only 10 comparisons Dividing 1023 by 2: values 511, 255, 127, 63, 31, 15, 7, 3, 1 and 0. The number 1023 (210 1) is divided by 2 only 10 times to get the value 0, which indicates that there are no more elements to test A vector of 1,048,575 (220 1) elements takes a maximum of 20 comparisons A vector of over one billion elements takes a maximum of 30 comparisons to find the key Compare it to the average case of linear search 30 comparisons instead of 500 millions Sharif University of Technology Advanced Programming: Sessions 26 and 27: Data Structures & Algorithm 93 Sorting Algo...
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