Data Structures Unit-1 Chapter-2

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 9

Data Structures

Unit-1
Chapter-2
2.Searching

1. Linear Search.
2. Binary Search.
1.Linear Search
• Searching Algorithms are designed to check for an element or retrieve an element
from any data structure where it is stored.
• Linear search is the simplest method for searching.
• In Linear search technique of searching; the element is searched sequentially in the
list.
• This method can be performed on a sorted or an unsorted list.
1. Linear Search Algorithm
Step 1: First, read the search element (Target element) in the array.
Step 2: In the second step compare the search element with the first element in
the array.
Step 3: If both are matched, display “Target element is found” and terminate the
Linear Search function.
Step 4: If both are not matched, compare the search element with the next
element in the array.
Step 5: In this step, repeat steps 3 and 4 until the search (Target) element is
compared with the last element of the array.
Step 6 – If the last element in the list does not match, the Linear Search Function
will be terminated, and the message “Element is not found” will be displayed.
1.Complexity of Linear Search
• Linear Search executes in O(n) times, where n is the number of
elements in the array.
• Its best execution time is 1, whereas the worst execution time is n,
where n is the total number of items in the search array.
2. Binary Search
• Binary search is the search technique that works efficiently on sorted lists. Hence,
to search an element into some list using the binary search technique, we must
ensure that the list is sorted.
• Binary search follows the divide and conquer approach in which the list is divided
into two halves, and the item is compared with the middle element of the list. If
the match is found then, the location of the middle element is returned.
Otherwise, we search into either of the halves depending upon the result
produced through the match.
• Eg:-Let us assume that we need to search the location of value 31
using binary search.
2. Binary Search
• First, we shall determine half of the array by using this formula −
mid=beg+end/2
Here it is, 0 + 9 / 2 = 4 (integer value of 4.5). So, 4 is the mid of the array.
2. Binary Search Algorithm
• Step 1: set beg = lower_bound, end = upper_bound, pos = - 1
• Step 2: repeat steps 3 and 4 while beg <=end
• Step 3: set mid = (beg + end)/2
• Step 4: if a[mid] = val
• set pos = mid
• print pos
• go to step 6
• else if a[mid] > val
• set end = mid - 1
• else
• set beg = mid + 1
• [end of if]
• [end of loop]
• Step 5: if pos = -1
• print "value is not present in the array"
• [end of if]
• Step 6: exit
2.Complexity of Binary Search
• Best Case Complexity - In Binary search, best case occurs when the
element to search is found in first comparison, i.e., when the first middle
element itself is the element to be searched. The best-case time
complexity of Binary search is O(1).
• Average Case Complexity - The average case time complexity of Binary
search is O(logn).
• Worst Case Complexity - In Binary search, the worst case occurs, when
we have to keep reducing the search space till it has only one element.
The worst-case time complexity of Binary search is O(logn).

You might also like