Course instructor: mam nadiya



Download 143.46 Kb.
Page2/3
Date05.06.2023
Size143.46 Kb.
#61475
1   2   3
dsa assign
Lab 1



What is Linear Search


Linear search is a simple searching algorithm. Here, the searching occurs from one item after the other. That is; this algorithm checks every item and checks for a matching item of that. If the item is not present, searching continues until the end of the data. Therefore, linear search is an algorithm that allows going through each element in an array to locate the given item.
In a linear search, the time consumption or the number of comparisons to search an element helps to determine the efficiency of the algorithm. If the element we search is in the first position of the data structure, it requires only one comparison. When the required element is in the last position, it requires an N number of comparisons to find the element. Here, the N refers to the number of data items.

What is Binary Search


Binary search is a fast algorithm. But, it is necessary to sort the data items before performing a binary search. It finds the item by comparing the middle most item of the collection. Hence, the binary search takes less time to search a given item with less number of comparisons as it involves finding the middle element and comparing the middle element with the element to search.


In a binary search, if the middle element is the required element, that index returns. If the middle item is higher than the searched item, then the searched item is in the left subarray of the middle item. Otherwise, the items are in the right subarray of the middle item. And, this process continues on the subarray until the subarray size becomes zero. In this algorithm, the number of items to search reduces each time.

Difference Between Linear Search and Binary Search

Definition


Linear search is an algorithm to find an element in a list by sequentially checking the elements of the list until finding the matching element. Binary search is an algorithm that finds the position of a target value within a sorted array. Thus, this is the main difference between linear search and binary search.



Download 143.46 Kb.

Share with your friends:
1   2   3




The database is protected by copyright ©ininet.org 2024
send message

    Main page