0% found this document useful (0 votes)
12 views2 pages

Searching

Linear search is a straightforward algorithm that finds an element in a list by checking each element sequentially. It has a best-case time complexity of O(1), a worst-case of O(n), and an average-case of O(n), with space complexity of O(1) for iterative and O(n) for recursive approaches. The algorithm continues until a match is found or all elements are checked, in which case it indicates the element is not present.

Uploaded by

srinu vas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

Searching

Linear search is a straightforward algorithm that finds an element in a list by checking each element sequentially. It has a best-case time complexity of O(1), a worst-case of O(n), and an average-case of O(n), with space complexity of O(1) for iterative and O(n) for recursive approaches. The algorithm continues until a match is found or all elements are checked, in which case it indicates the element is not present.

Uploaded by

srinu vas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Linear search:

Linear Search is a simple searching algorithm used to find an element in a list or array by
checking each element one by one. It works for both sorted and unsorted data.

Linear Search Algorithm

The algorithm for linear search is relatively simple. The procedure starts at the very first index of the
input array to be searched.

Step 1 − Start from the 0th index of the input array, compare the key value with the value present in
the 0th index.

Step 2 − If the value matches with the key, return the position at which the value was found.

Step 3 − If the value does not match with the key, compare the next element in the array.

Step 4 − Repeat Step 3 until there is a match found. Return the position at which the match was
found.

Step 5 − If it is an unsuccessful search, print that the element is not present in the array and exit the
program.

Time Complexity Analysis

 Best Case: O(1) → When the element is found at the beginning.

 Worst Case: O(n) → When the element is at the end or not present.

 Average Case: O(n) → On average, we search half the elements.

Space Complexity

 Iterative approach: O(1) (No extra space used)


 Recursive approach: O(n) (Due to function call stack)

You might also like