{"id":584,"date":"2020-05-21T05:55:48","date_gmt":"2020-05-21T05:55:48","guid":{"rendered":"http:\/\/learnlearn.uk\/alevelcs\/?page_id=584"},"modified":"2020-05-22T07:10:19","modified_gmt":"2020-05-22T07:10:19","slug":"linear-search-algorithm","status":"publish","type":"page","link":"https:\/\/learnlearn.uk\/alevelcs\/linear-search-algorithm\/","title":{"rendered":"Search Algorithms"},"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><span style=\"color: #0000ff;\">AS &amp; A Level &#8211; You are required to know how it works and be able to write Code \/ Pseudocode for the algorithm<\/span><\/p>\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\"><\/iframe><\/p>\n<p><em>How does my implementation above differ to standard Python in the way it handles linear search?<\/em><\/p>\n\n<\/div><h2 class=\"tabtitle\">Binary Search<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Binary Search<\/h3>\n<p><span style=\"color: #0000ff;\">A Level Only &#8211; You are required to know how it works and be able to write Code \/ Pseudocode for the algorithm<\/span><\/p>\n<p>Linear search is very effective but it is also quite inefficient, especially for very large arrays. If the array in question is an ordered array where all the items have been sorted, then an alternative such as Binary search can be used instead, which is far more efficient for larger arrays because it uses a divide and conquer methodology.<\/p>\n<div id=\"attachment_612\" style=\"width: 810px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/learnlearn.uk\/alevelcs\/wp-content\/uploads\/sites\/20\/2020\/05\/sorted-array-of-cards.jpg\"><img aria-describedby=\"caption-attachment-612\" decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-612\" src=\"https:\/\/learnlearn.uk\/alevelcs\/wp-content\/uploads\/sites\/20\/2020\/05\/sorted-array-of-cards.jpg\" alt=\"\" width=\"800\" height=\"181\" srcset=\"https:\/\/learnlearn.uk\/alevelcs\/wp-content\/uploads\/sites\/20\/2020\/05\/sorted-array-of-cards.jpg 800w, https:\/\/learnlearn.uk\/alevelcs\/wp-content\/uploads\/sites\/20\/2020\/05\/sorted-array-of-cards-300x68.jpg 300w, https:\/\/learnlearn.uk\/alevelcs\/wp-content\/uploads\/sites\/20\/2020\/05\/sorted-array-of-cards-768x174.jpg 768w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/a><p id=\"caption-attachment-612\" class=\"wp-caption-text\">You would be able to perform Binary search on this array of cards as it is ordered.<\/p><\/div>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n\n<\/div><h2 class=\"tabtitle\">Performance<\/h2>\n<div class=\"tabcontent\">\n\n<p>Factors affecting search performance<\/p>\n<ul>\n<li>Ordering of the items in the array.<\/li>\n<li>Size of the array<\/li>\n<li>Choice of algorithm<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n\n<\/div><h2 class=\"tabtitle\">Exam<\/h2>\n<div class=\"tabcontent\">\n\n<p><b>Practice Exam Questions<\/b><\/p>\n<p><a href=\"https:\/\/www.cambridgeinternational.org\/Images\/129852-2015-paper-4-specimen-paper.pdf\">2015 Specimen Paper<\/a> \/ <a href=\"https:\/\/www.cambridgeinternational.org\/Images\/129804-2015-paper-4-specimen-paper-markscheme.pdf\">Mark Scheme<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>Factors affecting search performance &#8211; initial data order, choice of search algorithm, size of array<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/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-algorithm\/\" class=\"\" rel=\"bookmark\">Read More &raquo;<span class=\"screen-reader-text\">Search Algorithms<\/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":"on","neve_meta_content_width":80,"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>Search Algorithms - 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-algorithm\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Search Algorithms - 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;Search Algorithms\" \/>\n<meta property=\"og:url\" content=\"https:\/\/learnlearn.uk\/alevelcs\/linear-search-algorithm\/\" \/>\n<meta property=\"og:site_name\" content=\"A Level Computer Science\" \/>\n<meta property=\"article:modified_time\" content=\"2020-05-22T07:10:19+00:00\" \/>\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-algorithm\/\",\"url\":\"https:\/\/learnlearn.uk\/alevelcs\/linear-search-algorithm\/\",\"name\":\"Search Algorithms - A Level Computer Science\",\"isPartOf\":{\"@id\":\"https:\/\/learnlearn.uk\/alevelcs\/#website\"},\"datePublished\":\"2020-05-21T05:55:48+00:00\",\"dateModified\":\"2020-05-22T07:10:19+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/learnlearn.uk\/alevelcs\/linear-search-algorithm\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/learnlearn.uk\/alevelcs\/linear-search-algorithm\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/learnlearn.uk\/alevelcs\/linear-search-algorithm\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"A Level Computer Science Home\",\"item\":\"https:\/\/learnlearn.uk\/alevelcs\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Search Algorithms\"}]},{\"@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":"Search Algorithms - 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-algorithm\/","og_locale":"en_GB","og_type":"article","og_title":"Search Algorithms - 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;Search Algorithms","og_url":"https:\/\/learnlearn.uk\/alevelcs\/linear-search-algorithm\/","og_site_name":"A Level Computer Science","article_modified_time":"2020-05-22T07:10:19+00:00","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-algorithm\/","url":"https:\/\/learnlearn.uk\/alevelcs\/linear-search-algorithm\/","name":"Search Algorithms - A Level Computer Science","isPartOf":{"@id":"https:\/\/learnlearn.uk\/alevelcs\/#website"},"datePublished":"2020-05-21T05:55:48+00:00","dateModified":"2020-05-22T07:10:19+00:00","breadcrumb":{"@id":"https:\/\/learnlearn.uk\/alevelcs\/linear-search-algorithm\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/learnlearn.uk\/alevelcs\/linear-search-algorithm\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/learnlearn.uk\/alevelcs\/linear-search-algorithm\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"A Level Computer Science Home","item":"https:\/\/learnlearn.uk\/alevelcs\/"},{"@type":"ListItem","position":2,"name":"Search Algorithms"}]},{"@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\/584"}],"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=584"}],"version-history":[{"count":34,"href":"https:\/\/learnlearn.uk\/alevelcs\/wp-json\/wp\/v2\/pages\/584\/revisions"}],"predecessor-version":[{"id":633,"href":"https:\/\/learnlearn.uk\/alevelcs\/wp-json\/wp\/v2\/pages\/584\/revisions\/633"}],"wp:attachment":[{"href":"https:\/\/learnlearn.uk\/alevelcs\/wp-json\/wp\/v2\/media?parent=584"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}