מהו חיפוש לינארי?
נניח שקיבלתם רשימה או מערך פריטים. אתה מחפש פריט מסוים. איך אתה עושה את זה?
מצא את המספר 13 ברשימה הנתונה.

אתה רק מסתכל על הרשימה והנה!

עכשיו, איך אתה אומר למחשב למצוא אותו?
מחשב אינו יכול להסתכל על יותר מהערך ברגע זמן נתון. אז זה לוקח פריט אחד מהמערך ובודק אם זהה למה שאתה מחפש.

הפריט הראשון לא תאם. אז עבור אל הבא.

וכן הלאה…
זה נעשה עד שנמצא התאמה או עד שכל הפריטים נבדקו.

באלגוריתם זה ניתן לעצור כאשר נמצא הפריט ואז אין צורך לחפש הלאה.
אז כמה זמן ייקח לבצע פעולת חיפוש לינארית? במקרה הטוב, יתמזל מזלך והפריט שאתה מסתכל אולי במיקום הראשון במערך!
אך במקרה הגרוע ביותר, תצטרך להסתכל על כל פריט ופריט לפני שתמצא את הפריט במקום האחרון או לפני שתבין שהפריט אינו נמצא במערך.
לכן המורכבות של חיפוש לינארי היא O (n).
אם האלמנט לחיפוש חי על גוש הזיכרון הראשון, המורכבות תהיה: O (1).
הקוד לפונקציית חיפוש ליניארית ב- JavaScript מוצג להלן. פונקציה זו מחזירה את מיקום הפריט אותו אנו מחפשים במערך. אם הפריט אינו קיים במערך, הפונקציה תחזיר null.
דוגמא ב- Javascript
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; }
דוגמא ברובי
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
דוגמה ב- C ++
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