Meta tags:
Headings (most frequently used words):
algorithm, linear, search, contents, analysis, application, see, also, references, basic, with, sentinel, in, an, ordered, table, non, uniform, probabilities, citations, works,
Text of the page (most frequently used words):
the (103), #search (54), list (37), and (25), return (22), linear (20), edit (15), then (15), for (14), this (13), #algorithm (13), case (13), are (12), displaystyle (11), value (10), element (9), with (8), when (8), can (8), comparisons (8), length (8), target (8), wikipedia (7), searching (7), knuth (7), else (7), that (7), probabilities (7), terminates (7), use (6), 1998 (6), sequential (6), subsection (6), expected (6), cost (6), move (6), worst (6), function (6), toggle (5), table (5), may (5), page (5), all (5), references (5), from (5), performance (5), other (5), faster (5), than (5), only (5), one (5), leq (5), likely (5), end (5), step (5), sentinel (5), contents (4), using (4), algorithms (4), binary (4), items (4), data (4), searched (4), more (4), single (4), not (4), approach (4), opt (4), frac (4), either (4), occurs (4), once (4), given (4), iterative (4), unsuccessfully (4), each (4), article (4), hide (4), sidebar (4), view (3), additional (3), terms (3), non (3), articles (3), short (3), index (3), isbn (3), computer (3), comparison (3), citations (3), makes (3), time (3), sort (3), values (3), order (3), example (3), has (3), elements (3), probability (3), known (3), equally (3), number (3), needed (3), analysis (3), define (3), pseudocode (3), below (3), recursive (3), successfully (3), increase (3), set (3), ordered (3), basic (3), until (3), sequentially (3), average (3), tools (3), main (3), languages (2), contact (2), about (2), privacy (2), policy (2), under (2), was (2), needing (2), november (2), 2010 (2), description (2), wikidata (2), retrieved (2), 201 (2), 89685 (2), vol (2), addison (2), wesley (2), art (2), programming (2), sorting (2), donald (2), works (2), theory (2), hash (2), see (2), also (2), even (2), arrays (2), large (2), because (2), initial (2), many (2), have (2), method (2), content (2), structure (2), simple (2), practical (2), application (2), general (2), arranged (2), decreasing (2), natural (2), assumption (2), requested (2), two (2), self (2), where (2), its (2), sequence (2), over (2), beginning (2), uniform (2), being (2), sought (2), orderings (2), most (2), cases (2), mbox (2), best (2), which (2), recursivetablesearch (2), such (2), recursivesentinelsearch (2), check (2), equals (2), adding (2), within (2), unsuccessful (2), recursivelinearsearch (2), find (2), checks (2), but (2), learn (2), help (2), sources (2), appearance (2), upload (2), file (2), changes (2), links (2), history (2), read (2), english (2), bahasa (2), log (2), create (2), account (2), donate (2), menu (2), add, topic, mobile, cookie, statement, statistics, developers, code, conduct, legal, safety, contacts, disclaimers, text, available, apply, site, you, agree, registered, trademark, profit, organization, wikimedia, foundation, inc, creative, commons, attribution, sharealike, license, rendered, parsoid, last, edited, june, 2026, utc, hidden, categories, different, category, https, org, php, title, linear_search, oldid, 1359501467, 2nd, reading, professional, horvath, adam, 2013, april, net, mono, platform, baeza, yates, ricardo, poblete, patricio, 1999, chapter, atallah, crc, press, 0849326494, computation, handbook, 1997, section, 3rd, 408, 396, keys, problem, ternary, result, though, instance, practice, medium, sized, around, 100, less, might, infeasible, anything, larger, sense, methods, enough, prepare, comparable, searches, same, often, pays, preprocess, build, efficient, should, change, frequently, repeated, reorganization, trouble, worth, usually, very, implement, few, performing, unordered, advance, cannot, spend, towards, head, they, heuristics, adjustment, trades, places, predecessor, access, independent, accesses, averaged, orders, satisfies, averaging, operations, note, among, sequences, satisfying, while, bad, amortized, transpose, front, adjusting, sum, ip_, particular, these, geometrically, distributed, improves, desired, near, therefore, some, much, others, desirable, place, them, way, both, asymptotically, corresponding, construct, however, begin, 5pt, times, equal, first, iterativetablesearch, establish, absence, quickly, concluding, exceeds, variation, requires, greater, iterativesentinelsearch, above, per, iteration, still, points, valid, extra, record, second, eliminated, making, will, reach, contained, iterativelinearsearch, otherwise, following, uses, subroutine, records, finds, matches, reaches, runs, affected, vary, rarely, schemes, allow, significantly, lists, tables, finding, match, found, whole, been, science, yes, optimal, space, complexity, class, how, remove, message, please, unsourced, material, challenged, jstor, scholar, books, newspapers, news, removed, reliable, improve, relies, source, looking, array, confused, line, free, encyclopedia, item, projects, printable, version, download, pdf, print, export, switch, legacy, parser, get, shortened, url, cite, information, permanent, link, related, what, here, actions, talk, tiếng, việt, українська, svenska, српски, srpski, slovenčina, русский, português, polski, nederlands, melayu, 한국어, ქართული, 日本語, italiano, íslenska, indonesia, magyar, हिन्दी, français, suomi, فارسی, español, ελληνικά, deutsch, dansk, čeština, বাংলা, azərbaycanca, العربية, top, personal, special, pages, recent, community, portal, contribute, random, current, events, navigation, jump,
Text of the page (random words):
linear search wikipedia jump to content main menu main menu move to sidebar hide navigation main page contents current events random article about wikipedia contact us contribute help learn to edit community portal recent changes upload file special pages search search appearance donate create account log in personal tools donate create account log in contents move to sidebar hide top 1 algorithm toggle algorithm subsection 1 1 basic algorithm 1 2 with a sentinel 1 3 in an ordered table 2 analysis toggle analysis subsection 2 1 non uniform probabilities 3 application 4 see also 5 references toggle references subsection 5 1 citations 5 2 works toggle the table of contents linear search 32 languages العربية azərbaycanca বাংলা čeština dansk deutsch ελληνικά español فارسی suomi français हिन्दी magyar bahasa indonesia íslenska italiano 日本語 ქართული 한국어 bahasa melayu nederlands polski português русский simple english slovenčina српски srpski svenska українська tiếng việt 粵語 中文 edit links article talk english read edit view history tools tools move to sidebar hide actions read edit view history general what links here related changes upload file permanent link page information cite this page get shortened url switch to legacy parser print export download as pdf printable version in other projects wikidata item appearance move to sidebar hide from wikipedia the free encyclopedia not to be confused with line search sequentially looking in an array this article relies on a single source please help improve this article by adding citations to reliable sources unsourced material may be challenged and removed find sources linear search news newspapers books scholar jstor november 2010 learn how and when to remove this message linear search class search algorithm worst case performance o n best case performance o 1 average performance o n worst case space complexity o 1 iterative optimal yes in computer science linear search or sequential search is a method for finding an element within a list it sequentially checks each element of the list until a match is found or the whole list has been searched 1 a linear search runs in linear time in the worst case and makes at most n comparisons where n is the length of the list if each element is equally likely to be searched then linear search has an average case of n 1 2 comparisons but the average case can be affected if the search probabilities for each element vary linear search is rarely practical because other search algorithms and schemes such as the binary search algorithm and hash tables allow significantly faster searching for all but short lists 2 algorithm edit 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 1 basic algorithm edit given a list l of n elements with values or records l 0 l n 1 and target value t the following subroutine uses linear search to find the index of the target t in l 3 set i to 0 if l i t the search terminates successfully return i increase i by 1 if i n go to step 2 otherwise the search terminates unsuccessfully we can define this in pseudocode as given below using either an iterative or recursive approach function iterativelinearsearch list l t is for i 0 to length l do if l i t then return i return an unsuccessful value in this case 1 return 1 function recursivelinearsearch list l t i 0 is if l i t then return i if i length l then return 1 unsuccessful value return recursivelinearsearch l t i i 1 with a sentinel edit the basic algorithm above makes two comparisons per iteration one to check if l i equals t and the other to check if i still points to a valid index of the list by adding an extra record l n to the list a sentinel value that equals the target the second comparison can be eliminated until the end of the search making the algorithm faster the search will reach the sentinel if the target is not contained within the list 4 set i to 0 if l i t go to step 4 increase i by 1 and go to step 2 if i n the search terminates successfully return i else the search terminates unsuccessfully we can define this in pseudocode as given below using either an iterative or recursive approach function iterativesentinelsearch list l t is for i 0 to length l do if l i t then if i length l then return i else return 1 return 1 function recursivesentinelsearch list l t i 0 is if i length l then return 1 if l i t then return i return recursivesentinelsearch l t i i 1 in an ordered table edit if the list is ordered such that l 0 l 1 l n 1 the search can establish the absence of the target more quickly by concluding the search once l i exceeds the target this variation requires a sentinel that is greater than the target 5 set i to 0 if l i t go to step 4 increase i by 1 and go to step 2 if l i t the search terminates successfully return i else the search terminates unsuccessfully we can define this in pseudocode as given below using either an iterative or recursive approach function iterativetablesearch list l t is for i 0 to length l do if l i t then if l i t then return i else return 1 return 1 function recursivetablesearch list l t i 0 is if i length l then return 1 if l i t then if l i t then return i else return 1 return recursivetablesearch l t i i 1 analysis edit for a list with n items the best case is when the value is equal to the first element of the list in which case only one comparison is needed the worst case is when the value is not in the list or occurs only once at the end of the list in which case n comparisons are needed if the value being sought occurs k times in the list and all orderings of the list are equally likely the expected number of comparisons is n if k 0 n 1 k 1 if 1 k n displaystyle begin cases n mbox if k 0 5pt displaystyle frac n 1 k 1 mbox if 1 leq k leq n end cases for example if the value being sought occurs once in the list and all orderings of the list are equally likely the expected number of comparisons is n 1 2 displaystyle frac n 1 2 however if it is known that it occurs once then at most n 1 comparisons are needed and the expected number of comparisons is n 2 n 1 2 n displaystyle displaystyle frac n 2 n 1 2n for example for n 2 this is 1 corresponding to a single if then else construct either way asymptotically the worst case cost and the expected cost of linear search are both o n non uniform probabilities edit the performance of linear search improves if the desired value is more likely to be near the beginning of the list than to its end therefore if some values are much more likely to be searched than others it is desirable to place them at the beginning of the list in particular when the list items are arranged in order of decreasing probability and these probabilities are geometrically distributed the cost of linear search is only o 1 6 in general if items are arranged in order of decreasing probability and the probability of searching for the i th element is p i displaystyle p_ i the expected cost of a single search is e s o p t i 1 n i p i displaystyle es opt sum _ i 1 n ip_ i under the natural assumption that the probabilities are not known in advance or one cannot spend the time to sort the list by probabilities one can use the approach of self adjusting data structure and move elements towards the head of the list when they are requested in a search two natural heuristics for this self adjustment are move to front mf and transpose t where the requested element trades places with its predecessor it is known that the expected cost of an access in a large sequence of independent accesses averaged over all initial orders of the list satisfies e s t e s m f π 2 e s o p t displaystyle es t leq es mf leq frac pi 2 es opt in terms of amortized cost averaging over a worst case sequence of operations note among sequences satisfying the assumption on probabilities we have s m f 2 s o p t displaystyle s mf leq 2s opt while s t displaystyle s t can be as bad as o m s o p t displaystyle o ms opt 7 application edit linear search is usually very simple to implement and is practical when the list has only a few elements or when performing a single search in an unordered list when many values have to be searched in the same list it often pays to preprocess the list in order to use a faster method for example one may sort the list and use binary search or build an efficient search data structure from it should the content of the list change frequently repeated reorganization may be more trouble than it is worth as a result even though in theory other search algorithms may be faster than linear search for instance binary search in practice even on medium sized arrays around 100 items or less it might be infeasible to use anything else on larger arrays it only makes sense to use other faster search methods if the data is large enough because the initial time to prepare sort the data is comparable to many linear searches 8 see also edit ternary search hash table linear search problem references edit citations edit 1 2 knuth 1998 6 1 sequential search knuth 1998 6 2 searching by comparison of keys knuth 1998 6 1 sequential search subsection algorithm b knuth 1998 6 1 sequential search subsection algorithm q knuth 1998 6 1 sequential search subsection algorithm t knuth donald 1997 section 6 1 sequential searching sorting and searching the art of computer programming vol 3 3rd ed addison wesley pp 396 408 isbn 0 201 89685 0 baeza yates ricardo poblete patricio v 1999 chapter 2 searching in atallah ed algorithms and theory of computation handbook crc press pp 2 3 isbn 0849326494 horvath adam binary search and linear search performance on the net and mono platform retrieved 19 april 2013 works edit knuth donald 1998 sorting and searching the art of computer programming vol 3 2nd ed reading ma addison wesley professional isbn 0 201 89685 0 retrieved from https en wikipedia org w index php title linear_search oldid 1359501467 category search algorithms hidden categories articles with short description short description is different from wikidata articles needing additional references from november 2010 all articles needing additional references this page was last edited on 15 june 2026 at 17 05 utc page was rendered with parsoid text is available under the creative commons attribution sharealike 4 0 license additional terms may apply by using this site you agree to the terms of use and privacy policy wikipedia is a registered trademark of the wikimedia foundation inc a non profit organization privacy policy about wikipedia disclaimers contact wikipedia legal safety contacts code of conduct developers statistics cookie statement mobile view search search toggle the table of contents linear search 32 languages add topic
|