Mi az a lineáris keresés?
Tegyük fel, hogy megad egy listát vagy egy tömböt. Egy adott elemre keres. Hogyan csinálod, hogy?
Keresse meg a 13-as számot a megadott listában.

Csak nézd meg a listát, és ott van!

Most hogyan mondhatja el a számítógépnek, hogy találja meg?
A számítógép nem tud többet nézni, mint az adott pillanat pillanatának értéke. Tehát elvesz egy elemet a tömbből, és ellenőrzi, hogy megegyezik-e azzal, amit keres.

Az első tétel nem egyezett. Tehát lépj a következőre.

Stb…
Ez addig történik, amíg egyezést nem találnak, vagy amíg az összes elemet nem ellenőrzik.

Ebben az algoritmusban leállhat, amikor az elem megtalálható, és akkor nincs szükség további keresésre.
Tehát mennyi időbe telik a lineáris keresési művelet elvégzése? A legjobb esetben szerencsés lehet, és az éppen megtekintett tárgy talán a tömb első pozíciójában van!
De a legrosszabb esetben meg kell néznie minden egyes elemet, mielőtt megtalálja az elemet az utolsó helyen, vagy mielőtt rájönne, hogy az elem nincs a tömbben.
A lineáris keresés bonyolultsága tehát O (n).
Ha a keresendő elem az első memóriablokkban élne, akkor a komplexitás a következő lenne: O (1).
Az alábbiakban látható a lineáris keresési függvény kódja a JavaScript-ben. Ez a függvény visszaadja a keresett elem pozícióját a tömbben. Ha az elem nincs a tömbben, a függvény null értéket ad vissza.
Példa Javascriptben
function linearSearch(arr, item) { // Go through all the elements of arr to look for item. for (var i = 0; i < arr.length; i++) { if (arr[i] === item) { // Found it! return i; } } // Item not found in the array. return null; }
Példa a Ruby-ban
def linear_search(target, array) counter = 0 while counter < array.length if array[counter] == target return counter else counter += 1 end end return nil end
Példa a C ++ - ban
int linear_search(int arr[],int n,int num) { for(int i=0;i
Example in Python
def linear_search(array, num): for i in range(len(array)): if (array[i]==num): return i return -1
Global Linear Search
What if you are searching the multiple occurrences of an element? For example you want to see how many 5’s are in an array.
Target = 5
Array = [ 1, 2, 3, 4, 5, 6, 5, 7, 8, 9, 5]
This array has 3 occurrences of 5s and we want to return the indexes (where they are in the array) of all of them.
This is called global linear search and you will need to adjust your code to return an array of the index points at which it finds your target element.
When you find an index element that matches your target, the index point (counter) will be added in the results array. If it doesn’t match, the code will continue to move on to the next element in the array by adding 1 to the counter.
def global_linear_search(target, array) counter = 0 results = [] while counter < array.length if array[counter] == target results << counter counter += 1 else counter += 1 end end if results.empty? return nil else return results end end
Why linear search is not efficient
There is no doubt that linear search is simple. But because it compares each element one by one, it is time consuming and therefore not very efficient. If we have to find a number from, say, 1,000,000 numbers and that number is at the last position, a linear search technique would become quite tedious.
So you should also learn about bubble sort, quick sort and other more efficient algorithms.
Other search algorithms:
How to implement quick sort
Binary search algorithm
Jump search algorithm
Search algorithms explained with examples
Implement a binary search algorithm in Java without recursion
More info on search algorithms

Original text