{"id":2461,"date":"2023-11-03T09:19:29","date_gmt":"2023-11-03T09:19:29","guid":{"rendered":"https:\/\/learnlearn.uk\/alevelcs\/?page_id=2461"},"modified":"2023-11-03T09:19:29","modified_gmt":"2023-11-03T09:19:29","slug":"linear-search","status":"publish","type":"page","link":"https:\/\/learnlearn.uk\/alevelcs\/linear-search\/","title":{"rendered":"Linear Search"},"content":{"rendered":"<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Search Algorithms<\/h2>\n<div class=\"tabcontent\">\n\n<h3>What are searching algorithms?<\/h3>\n<p>Search algorithms are algorithms designed to find items in an an array(list). If the item is found in the search the the algorithm will return the index(position) of the item in the array. If an array contains duplicates of an item being searched for it will normally return the index of the first instance that it finds.<\/p>\n<p><strong>Example<\/strong><\/p>\n<p>In the array of cards below , if you searched for the item &#8216;4 of clubs&#8217;, the algorithm would return the integer 1.<\/p>\n<div id=\"attachment_605\" style=\"width: 710px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/learnlearn.uk\/alevelcs\/wp-content\/uploads\/sites\/20\/2020\/05\/searching-algorithms-cie-a-level-9618-v2.jpg\"><img aria-describedby=\"caption-attachment-605\" decoding=\"async\" loading=\"lazy\" class=\"wp-image-605 size-full\" src=\"https:\/\/learnlearn.uk\/alevelcs\/wp-content\/uploads\/sites\/20\/2020\/05\/searching-algorithms-cie-a-level-9618-v2.jpg?_t=1590114506\" alt=\"\" width=\"700\" height=\"231\" srcset=\"https:\/\/learnlearn.uk\/alevelcs\/wp-content\/uploads\/sites\/20\/2020\/05\/searching-algorithms-cie-a-level-9618-v2.jpg 700w, https:\/\/learnlearn.uk\/alevelcs\/wp-content\/uploads\/sites\/20\/2020\/05\/searching-algorithms-cie-a-level-9618-v2-300x99.jpg 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/a><p id=\"caption-attachment-605\" class=\"wp-caption-text\">Note how indexing begins at 0, not 1.<\/p><\/div>\n<p><em>*Some languages, such as Scratch would return 2, as they start counting at 1 instead of 0.<\/em><\/p>\n<p>&nbsp;<\/p>\n<div class=\"arconix-box arconix-box-lblue\"><div class=\"arconix-box-content\">\n<p><strong>What happens if the item is not in the array?<\/strong><\/p>\n<p>If the item is not found then depending on the programming different things will happen:<\/p>\n<ul>\n<li>Python &#8211; It will raise an exception (ERROR!!!)<\/li>\n<li>JavaScript &#8211; It will return -1 (JavaScript arrays start indexing from zero)<\/li>\n<li>Scratch &#8211; It return Zero (Scratch lists are 1 based because it&#8217;s a blocks based language designed for children)<\/li>\n<\/ul>\n<\/div><\/div>\n\n<\/div><h2 class=\"tabtitle\">Linear Search<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Linear Search<\/h3>\n<p>The linear search(a.k.a sequential search) algorithm is a simple search algorithm that starts at the left hand side of an array (index 0) and moves through the array one item at a time. Once the item being searched for is found the algorithm returns the index of the item in question. If the algorithm reaches the end of the array without finding the item then it either returns an error or it returns a non valid index depending on the implementation.<\/p>\n<h3>Example Python Code Implementation of Linear Search<\/h3>\n<p><iframe loading=\"lazy\" src=\"https:\/\/trinket.io\/embed\/python\/6e4c6ca1f0?start=result\" width=\"100%\" height=\"600\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" allowfullscreen=\"allowfullscreen\" data-mce-fragment=\"1\"><\/iframe><\/p>\n<p><em>How does my implementation above differ to standard Python in the way it handles linear search?<\/em><\/p>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>What are searching algorithms? Search algorithms are algorithms designed to find items in an an array(list). If the item is found in the search the the algorithm will return the index(position) of the item in the array. If an array contains duplicates of an item being searched for it will normally return the index of&hellip;&nbsp;<a href=\"https:\/\/learnlearn.uk\/alevelcs\/linear-search\/\" class=\"\" rel=\"bookmark\">Read More &raquo;<span class=\"screen-reader-text\">Linear Search<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"off","neve_meta_content_width":100,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Linear Search - A Level Computer Science<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/learnlearn.uk\/alevelcs\/linear-search\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Linear Search - A Level Computer Science\" \/>\n<meta property=\"og:description\" content=\"What are searching algorithms? Search algorithms are algorithms designed to find items in an an array(list). If the item is found in the search the the algorithm will return the index(position) of the item in the array. If an array contains duplicates of an item being searched for it will normally return the index of&hellip;&nbsp;Read More &raquo;Linear Search\" \/>\n<meta property=\"og:url\" content=\"https:\/\/learnlearn.uk\/alevelcs\/linear-search\/\" \/>\n<meta property=\"og:site_name\" content=\"A Level Computer Science\" \/>\n<meta property=\"og:image\" content=\"https:\/\/learnlearn.uk\/alevelcs\/wp-content\/uploads\/sites\/20\/2020\/05\/searching-algorithms-cie-a-level-9618-v2.jpg?_t=1590114506\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/learnlearn.uk\/alevelcs\/linear-search\/\",\"url\":\"https:\/\/learnlearn.uk\/alevelcs\/linear-search\/\",\"name\":\"Linear Search - A Level Computer Science\",\"isPartOf\":{\"@id\":\"https:\/\/learnlearn.uk\/alevelcs\/#website\"},\"datePublished\":\"2023-11-03T09:19:29+00:00\",\"dateModified\":\"2023-11-03T09:19:29+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/learnlearn.uk\/alevelcs\/linear-search\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/learnlearn.uk\/alevelcs\/linear-search\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/learnlearn.uk\/alevelcs\/linear-search\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"A Level Computer Science Home\",\"item\":\"https:\/\/learnlearn.uk\/alevelcs\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Linear Search\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/learnlearn.uk\/alevelcs\/#website\",\"url\":\"https:\/\/learnlearn.uk\/alevelcs\/\",\"name\":\"A Level Computer Science\",\"description\":\"CIE Specification\",\"publisher\":{\"@id\":\"https:\/\/learnlearn.uk\/alevelcs\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/learnlearn.uk\/alevelcs\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/learnlearn.uk\/alevelcs\/#organization\",\"name\":\"A Level Computer Science\",\"url\":\"https:\/\/learnlearn.uk\/alevelcs\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\/\/learnlearn.uk\/alevelcs\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/learnlearn.uk\/alevelcs\/wp-content\/uploads\/sites\/20\/2019\/09\/LearnLearnLogowhite.png\",\"contentUrl\":\"https:\/\/learnlearn.uk\/alevelcs\/wp-content\/uploads\/sites\/20\/2019\/09\/LearnLearnLogowhite.png\",\"width\":710,\"height\":98,\"caption\":\"A Level Computer Science\"},\"image\":{\"@id\":\"https:\/\/learnlearn.uk\/alevelcs\/#\/schema\/logo\/image\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Linear Search - A Level Computer Science","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/learnlearn.uk\/alevelcs\/linear-search\/","og_locale":"en_GB","og_type":"article","og_title":"Linear Search - A Level Computer Science","og_description":"What are searching algorithms? Search algorithms are algorithms designed to find items in an an array(list). If the item is found in the search the the algorithm will return the index(position) of the item in the array. If an array contains duplicates of an item being searched for it will normally return the index of&hellip;&nbsp;Read More &raquo;Linear Search","og_url":"https:\/\/learnlearn.uk\/alevelcs\/linear-search\/","og_site_name":"A Level Computer Science","og_image":[{"url":"https:\/\/learnlearn.uk\/alevelcs\/wp-content\/uploads\/sites\/20\/2020\/05\/searching-algorithms-cie-a-level-9618-v2.jpg?_t=1590114506"}],"twitter_card":"summary_large_image","twitter_misc":{"Estimated reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/learnlearn.uk\/alevelcs\/linear-search\/","url":"https:\/\/learnlearn.uk\/alevelcs\/linear-search\/","name":"Linear Search - A Level Computer Science","isPartOf":{"@id":"https:\/\/learnlearn.uk\/alevelcs\/#website"},"datePublished":"2023-11-03T09:19:29+00:00","dateModified":"2023-11-03T09:19:29+00:00","breadcrumb":{"@id":"https:\/\/learnlearn.uk\/alevelcs\/linear-search\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/learnlearn.uk\/alevelcs\/linear-search\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/learnlearn.uk\/alevelcs\/linear-search\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"A Level Computer Science Home","item":"https:\/\/learnlearn.uk\/alevelcs\/"},{"@type":"ListItem","position":2,"name":"Linear Search"}]},{"@type":"WebSite","@id":"https:\/\/learnlearn.uk\/alevelcs\/#website","url":"https:\/\/learnlearn.uk\/alevelcs\/","name":"A Level Computer Science","description":"CIE Specification","publisher":{"@id":"https:\/\/learnlearn.uk\/alevelcs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/learnlearn.uk\/alevelcs\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-GB"},{"@type":"Organization","@id":"https:\/\/learnlearn.uk\/alevelcs\/#organization","name":"A Level Computer Science","url":"https:\/\/learnlearn.uk\/alevelcs\/","logo":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/learnlearn.uk\/alevelcs\/#\/schema\/logo\/image\/","url":"https:\/\/learnlearn.uk\/alevelcs\/wp-content\/uploads\/sites\/20\/2019\/09\/LearnLearnLogowhite.png","contentUrl":"https:\/\/learnlearn.uk\/alevelcs\/wp-content\/uploads\/sites\/20\/2019\/09\/LearnLearnLogowhite.png","width":710,"height":98,"caption":"A Level Computer Science"},"image":{"@id":"https:\/\/learnlearn.uk\/alevelcs\/#\/schema\/logo\/image\/"}}]}},"rttpg_featured_image_url":null,"rttpg_author":{"display_name":"learnlearnadmin","author_link":"https:\/\/learnlearn.uk\/alevelcs\/author\/learnlearnadmin\/"},"rttpg_comment":0,"rttpg_category":null,"rttpg_excerpt":"What are searching algorithms? Search algorithms are algorithms designed to find items in an an array(list). If the item is found in the search the the algorithm will return the index(position) of the item in the array. If an array contains duplicates of an item being searched for it will normally return the index of&hellip;&nbsp;Read&hellip;","_links":{"self":[{"href":"https:\/\/learnlearn.uk\/alevelcs\/wp-json\/wp\/v2\/pages\/2461"}],"collection":[{"href":"https:\/\/learnlearn.uk\/alevelcs\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/learnlearn.uk\/alevelcs\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/learnlearn.uk\/alevelcs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnlearn.uk\/alevelcs\/wp-json\/wp\/v2\/comments?post=2461"}],"version-history":[{"count":1,"href":"https:\/\/learnlearn.uk\/alevelcs\/wp-json\/wp\/v2\/pages\/2461\/revisions"}],"predecessor-version":[{"id":2462,"href":"https:\/\/learnlearn.uk\/alevelcs\/wp-json\/wp\/v2\/pages\/2461\/revisions\/2462"}],"wp:attachment":[{"href":"https:\/\/learnlearn.uk\/alevelcs\/wp-json\/wp\/v2\/media?parent=2461"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}