+ 1
Linear search in JavaScript
Although we do linear search using if else statement and indexOf(); very easily without using any loop .... Like this https://www.sololearn.com/post/904561/?ref=app Then why all tutorials teach to use loop for linear search ?
13 Réponses
+ 3
ℍ𝕒𝕣𝕣𝕪 💕 𝕻𝖔𝖙𝖙𝖊𝖗 ⚡✨
For your Non loop code above check this for an optimization where the indexOf() function is ran once.
var arr = [5, 7, 2, 9, 0, 7];
function search(k){
i = arr.indexOf(k);
if (i == -1)
return i;
return ++i;
}
console.log(search(7));
For your loop attempt if the if condition is false it returns -1. The very 1st check is false and no further checks are made.
Loop through the array and only do an if statement, no else, to return the index if found. Then after the loop return -1 to cover a not found.
function search(array, k){
for(let i = 0; i < array.length; i++){
if(array[i] === k)
return i+1;
}
return -1;
}
+ 8
Perhaps so that we can understand better what's going on behind the scene?
Many search algorithms implements loops behind the scene. The difference is, to either use built-in search feature provided, or to try implement it on our own, in which case we understand how a search algorithm works internally.
Although, in high level languages, use of built-in features is encouraged more over self implementation, in consideration of code reuse, and development period reduction.
Personally I see self implementation of search algorithm is more closely related with academic purposes. Not saying, it's bad, just that I have and am still going through constant surprise times finding out that what I learn to do apparently has been available, just my lack of knowledge of its presence : )
+ 4
That code isn't an actual linear search, but how do you think indexOf() works?
Try writing the code for indexOf() yourself and then you'll write a real linear search. FYI, that code could and should be optimized further by only calling indexOf() once instead of "searching" the array for the index 3 times which increases the runtime.
https://en.m.wikipedia.org/wiki/Linear_search#:~:text=In%20computer%20science%2C%20a%20linear,whole%20list%20has%20been%20searched.
+ 4
ℍ𝕒𝕣𝕣𝕪 💕 𝕻𝖔𝖙𝖙𝖊𝖗 ⚡✨
I'm not an expert of algorithms, so I am not in position to say. But for general cases, and in relation to high level languages, use of built-in search feature is always encouraged.
+ 3
Ipang
Yes, by using loops, we go deep into the algorithm to understand better..
But can I use this built-in indexOf(); for linear search in every case ?
+ 3
Got it now !
indexOf() and if else can be used for linear search without loop, if no element is repeated
It will show only first index of repeated element like this
https://code.sololearn.com/WzqxI7Eql0ck/?ref=app
we have to use loop, if the elements are repeating
Am I right ?
ChaoticDawg sir
Ipang sir
Kode Krasher sir
+ 3
ChaoticDawg
I understood sir
https://code.sololearn.com/WD8fOpFVQS7D/?ref=app
I tried, but it's showing -1 instead of actual index (5)
Sir, can you please tell me where is the error ?
+ 3
ℍ𝕒𝕣𝕣𝕪 💕 𝕻𝖔𝖙𝖙𝖊𝖗 ⚡✨
Here's documentation on `indexOf` method. After seeing this, you'd have an idea how to use it to find more values after the first one.
https://developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/array/indexof
+ 3
ChaoticDawg sir, I understood the loop attempt.
https://code.sololearn.com/WzqxI7Eql0ck/?ref=app
But couldn't understand the non-loop attempt
+ 2
ChaoticDawg
But it's showing correct answer in geeks for geeks
https://www.sololearn.com/post/904561/?ref=app
Look at the bottom of the image
+ 2
Just because the outputs are correct doesn't mean you accomplished the given task as intended
+ 2
ℍ𝕒𝕣𝕣𝕪 💕 𝕻𝖔𝖙𝖙𝖊𝖗 ⚡✨
When it comes to Javascript and performing a linear search on an array, yes, you should use the built-in indexOf(). However, with custom built data structures this may not work, so you may need to implement your own search for your custom structures etc. This is why you also need the knowledge of how to do so and how such functions are working behind the scenes.
Built-in functions are typically already optimized so they should be used when possible in your real world code. The intent for lessons such as the one you're doing is to expand your knowledge base and problem solving abilities by writing the code yourself. This is what I was attempting to hint at.
+ 2
ℍ𝕒𝕣𝕣𝕪 💕 𝕻𝖔𝖙𝖙𝖊𝖗 ⚡✨
It only makes 1 call to the indexOf() function, which is basically a linear search function itself. This saves on computational runtime. The indexOf() function will return -1 if the element is not found in the search, so we use an if statement to check if the returned value is -1 an return it if so. Otherwise the index + 1 is returned.