{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CS301-Lec39 handout - CS301 Data Structures Lecture No 39...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS301 – Data Structures Lecture No. 39 ___________________________________________________________________ Data Structures Lecture No. 39 Reading Material Data Structures and Algorithm Analysis in C++ Chapter. 10 10.4.2 Summary Searching an Array: Binary Search Binary Search - Example 1 Binary Search - Example 2 Binary Search - Example 3 Binary Search – C++ Code Binary Search – Binary Tree Binary Search - Efficiency Implementation 3 (of Table ADT): Linked List Implementation 4 (of Table ADT): Skip List Skip List - Representation Skip List - Higher Level Chains Skip List - Formally Searching an Array: Binary Search In the previous lecture, we had started discussion on Binary Search Tree algorithm. The discussion revealed that if already sorted data is available, then it is better to apply an algorithm of binary search for finding some item inside instead of searching from start to the end in sequence. The application of this algorithm will help get the results very quickly. We also talked about the example of directory of employees of a company where the names of the employee were sorted. For efficient searching, we constructed the binary search tree for the directory and looked for information about an employee named ‘Ahmed Faraz’. We also covered: - Binary search is like looking up a phone number or a word in the dictionary - Start in middle of book - If the name you're looking for, comes before names on the page, search in the first half - Otherwise, look into the second half Page 1 of 11 Fig 39.1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
CS301 – Data Structures Lecture No. 39 ___________________________________________________________________ The telephone directory is the quotable example to understand the way the binary search method works. In this lecture, we will focus on data structures for performing search operation. Consider the data is present in an array as we discussed in the previous lecture. For the first implementation, we supposed that the data is not sorted in the array. For second implementation, we considered that the data inside the array is put in sorted array. The advantage of the effort of putting the data in the array in sorted order pays off when the searches on data items are performed. Now, let’s first see the algorithm (in pseudo code) for this operation below. It is important to mention that this algorithm is independent of data type i.e. the data can be of any type numeric or string. if ( value == middle element ) value is found else if ( value < middle element ) search left half of list with the same method else search right half of list with the same method The item we are searching for in this algorithm is called value . The first comparison of this value is made with the middle element of the array. If both are equal, it means that we have found our desired search item, which is present in the middle of the array. If this is not the case, then the value and the middle element are not the same. The else-if part of the algorithm is computed, which checks if the value is less than the middle element . If so, the left half part of the array is searched further in the same fashion (of logically splitting that half part of array into two further halves and applying this algorithm again).
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}