2.4 KiB
Linear Search
linear search, also known as Sequential search is a method for finding an element within a list. A linear search sequentially checks each element of the list until it finds an element that matches the target value. If the algorithm reaches the end of the list, the search terminates unsuccessfully.
- Time Complexity: O(n)
- Space Complexity: O(1)
- Applications: practical when the list has only a few elements when performing a single search in an unordered list.
- Founder's Name: Anatole Beck and Donald J. Newman (1970) Linear search is usually very simple to implement.
Steps/Algorithm:
Linear Search( Array A, Value x)
Step 1: Set i to 1 Step 2: if i > n then go to step 7 Step 3: if A[i] = x then go to step 6 Step 4: Set i to i + 1 Step 5: Go to Step 2 Step 6: Print Element x Found at index i and go to step 8 Step 7: Print element not found Step 8: Exit
Pseudocode
procedure linear_search (list, value)
start procedure
for each item in the list
if match item == value
return the item's location
end if
end for
end procedure
Example
Input A[]: 10,20,30,40,50,60,70,80
Element to search: 50
Procedure: Step 1: i = 0 Step 2: Comparing A[i] and Element to search, if equal numbers return i, else i = i + 1. comparing 10 and 50 ->False, i = i + 1 Step 3: comparing 20 and 50 ->False, i = i + 1 Step 4: comparing 30 and 50 ->False, i = i + 1 Step 5: comparing 40 and 50 ->False, i = i + 1 Step 6: comparing 50 and 50 ->True -> Element Found
Output: i