Featured
- Get link
- X
- Other Apps
Search in python
Search is used to find a element is present in a given sequence of data or not. In python there are two types of search
1) Linear search
2) Binary search
Linear search:
Linear search is one of the most old method used for searching.linear search is used for unsorted elements. We learn it to understand the searching process done in the program. Linear search compares the search element with all the elements present in the sequence which leads to more time.
It starts from the first and search till the last element till the data is find sometimes the data may not be present in the sequence which leads to waste of process it is one of the disadvantage in linear search.
Best time complexity: 0[1]
Average time complexity: 0[n]
Worst time complexity: 0[n]
- Start from the leftmost element of given arr[] and one by one compare element x with each element of arr[]
- If x matches with any of the element, return the index value.
- If x doesn’t match with any of elements in arr[] , return -1 or element not found.
Example:
output :
Binary search:
Binary search is faster than linear search. Binary search is the simplest and easiest process. It is used only in sorted elements. The running time of binary search is log2 N and searching time N/2.
In binary search it first separate the sequence of data into two parts then it will check the number is present in part one or two the process will continue until the element is found due to this the process it doesn't take much time . The compiler don't want to check all the elements.
Best time complexity: 0[1]
Average time complexity: 0[log n]
Worst time complexity: 0[log n]
Lets see deeply about the algorithm and working process of binary search in next upcoming post!!!!
Hope you understand the program and try yourself for more clear view feel free to comment your thoughts!!
Book for python: https://amzn.to/3pCgIqy
ebook: https://amzn.to/3pHioiO
- Get link
- X
- Other Apps
Comments
Post a Comment