What is Searching?
Types of Searching
Linear Search:
Binary Search:
- Searching is the process of finding a given value position in a list of values.
- It decides whether a search key is present in the data or not.
- It is the algorithmic process of finding a particular item in a collection of items.
- It can be done on an internal data structure or on an external data structure.
Types of Searching
- There are 2 basic approaches: Sequential search and Binary search
Linear Search:
- A sequential search is also called as Linear Search.
- The sequential search starts at the beginning of the list and checks every element of the list.
- It is a basic and simple search algorithm.
- Sequential search compares the element with all the other elements given in the list. If the element is matched, it returns the value index, else it returns -1.
- Algorithms:
- It searches an element or value from an array until the desired element or value is not found.
- If we search the element 33, it will go step by step in sequence order.
- It searches in a sequence order.
- A sequential search is applied on the unsorted or unordered list when there are fewer elements in a list.
Binary Search:
- Binary Search is used for searching an element in a sorted array.
- It is a fast search algorithm with run-time complexity of O(log n).
- Binary search works on the principle of divide and conquer.
- This searching technique looks for a particular element by comparing the middle most element of the collection.
- It is useful when there are large number of elements in an array.
- The array is sorted in ascending order. As we know binary search is applied on sorted lists only for fast searching.
- Binary searching starts with middle element. If the element is equal to the element that we are searching then return true. If the element is less than then move to the right of the list or if the element is greater than then move to the left of the list. Repeat this, till you find an element.
- Algorithms:
Tags:
data structure