Linear search is a sequential searching algorithm where we start from one end and check every element of the list until the desired element is found. It is the simplest searching algorithm. Referfor better undeerstanding.
| Best | Worst | Memory |
|---|---|---|
| 1 | n | 1 |
LinearSearch( A : Array of items, Target : item to be searched)
for each i item in the array:
if item == target
return index i // Returns the index at wich the element is present
else return -1 // Denotes element is not present in that array.
int linearSearch(vector<int> v , int target){
for(int i = 0; i < v.size(); i++){
if(v[i] == target){
return i;
}
}
return -1;
}
def linearSearch(Array,target):
for i in range(0,len(Array)):
if Array[i] == target:
return i
return -1 int linearSearch(int array[],int target){
int size = sizeof(array)/sizeof(int);
for(int i = 0; i < size; i++){
if(array[i] == target) {
return i;
}
}
return -1;
} public static int linearSearch(int array [], int target){
int size = array.length;
for(int i = 0; i < size;i++){
if(array[i] == target){
return i;
}
}
return -1;
} const linearSearch = (list, item) => {
for (const [i, element] of list.entries()) {
if (element === item) {
return i
}
}
return -1
} func linearSearch(array []int, target int){
size := len(array)
for i := 0; i < size; i++{
if array[i] == target {
return i
}
}
return -1
} def linear_search(array, element)
i = 0
while i < array.length
if array[i] == element
return "#{element} at index #{array.index(element)}"
end
i+=1
end
return -1
end