{"id":715,"date":"2020-06-08T01:44:08","date_gmt":"2020-06-08T01:44:08","guid":{"rendered":"http:\/\/learnlearn.uk\/alevelcs\/?page_id=715"},"modified":"2023-03-13T07:41:26","modified_gmt":"2023-03-13T07:41:26","slug":"big-o-notation-searching-sorting","status":"publish","type":"page","link":"https:\/\/learnlearn.uk\/alevelcs\/big-o-notation-searching-sorting\/","title":{"rendered":"Big O Notation with Searching &#038; Sorting"},"content":{"rendered":"<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Introduction<\/h2>\n<div class=\"tabcontent\">\n\n<h3>What is Big O Notation?<\/h3>\n<p>Big O notation is used used as a tool to describe the growth rate of a function in terms of the number of instructions that need to be processed (time complexity) or the amount of memory required (space complexity).<\/p>\n<p>This allows different algorithms to be compared in terms of their efficiency.<\/p>\n<p>Note: Two algorithms can have the same Big O notation but in practice have wildly different execution times in practice. This is because the Big O describes the growth rate of complexity, not the actual complexity itself.<\/p>\n<p><strong>Time Complexity<\/strong><\/p>\n<p>Time complexity refers to the growth in the number of instructions executed (and therefore the time taken) as the length of the array to be searched increases.<\/p>\n<p><strong>Space Complexity<\/strong><\/p>\n<p>Space complexity refers to the growth in the size of memory space that is required as the length of the array is increased.<\/p>\n<p><strong>Algorithm Performance<\/strong><\/p>\n<p>There are a number of factors that affect the performance of search \/ sorting algorithms. Some algorithms perform well with high entropy (randomness) data, other algorithms work better when the data is partially sorted in some manner. This means that no one algorithm works best in every situation and the nature of the data being sorted needs to considered.<\/p>\n<p><iframe loading=\"lazy\" src=\"https:\/\/trinket.io\/embed\/python\/ff358a934a?toggleCode=true&amp;start=result\" width=\"100%\" height=\"600\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n\n<\/div><h2 class=\"tabtitle\">Bubble Sort<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Bubble Sort<\/h3>\n<p><span style=\"color: #0000ff;\">Time Complexity:\u00a0O(n\u00b2) &#8211; Space Complexity: O(1)<\/span><\/p>\n<p>Pretty much the worst sorting algorithm ever, mostly just used in teaching as a comparison tool. There are <a href=\"https:\/\/en.wikipedia.org\/wiki\/Bubble_sort\">almost <\/a>no legitimate use cases where it is the most efficient algorithm.<\/p>\n<p><iframe loading=\"lazy\" src=\"https:\/\/trinket.io\/embed\/python\/32bc633113\" width=\"100%\" height=\"600\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n\n<\/div><h2 class=\"tabtitle\">Insertion Sort<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Insertion Sort<\/h3>\n<p><span style=\"color: #0000ff;\">Time Complexity:\u00a0O(n\u00b2) &#8211; Space Complexity: O(1)<\/span><\/p>\n<p><iframe loading=\"lazy\" src=\"https:\/\/trinket.io\/embed\/python\/76246cc62c\" width=\"100%\" height=\"600\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n\n<\/div><h2 class=\"tabtitle\">Merge Sort<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Merge Sort<\/h3>\n<p><span style=\"color: #0000ff;\">Time Complexity: O(n log(n)) &#8211; Space Complexity: O(n)<\/span><\/p>\n<p><span style=\"color: #808080;\">Note: You are not required to know how to code a merge sort algorithm for your A-level exam, it is simply here for reference\/comparison as it is a n log(n) algorithm, rather than a n\u00b2 algorithm.<\/span><\/p>\n<div class=\"nv-iframe-embed\">\n<div class=\"container-lazyload preview-lazyload container-youtube js-lazyload--not-loaded\"><a href=\"https:\/\/www.youtube.com\/watch?v=TzeBrDU-JaY\" class=\"lazy-load-youtube preview-lazyload preview-youtube\" data-video-title=\"Merge sort algorithm\" title=\"Play video &quot;Merge sort algorithm&quot;\">https:\/\/www.youtube.com\/watch?v=TzeBrDU-JaY<\/a><noscript>Video can&#8217;t be loaded because JavaScript is disabled: <a href=\"https:\/\/www.youtube.com\/watch?v=TzeBrDU-JaY\" title=\"Merge sort algorithm\">Merge sort algorithm (https:\/\/www.youtube.com\/watch?v=TzeBrDU-JaY)<\/a><\/noscript><\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\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;\">Time Complexity: O(n) &#8211; Space Complexity O(1)<\/span><\/p>\n<p>Linear search is a simple searching algorithm that iterates through an array from left to right looking for a target item.<\/p>\n<ul>\n<li>If the target is found then it returns the index(location) of the item.<\/li>\n<li>If the target is not in the array then usually -1 is return (or None\/Null depending on the implementation)<\/li>\n<li>If multiple instances of the target are found, the index of the leftmost instance is returned)<\/li>\n<\/ul>\n<p>Linear search is generally considered to be quite inefficient, as the whole list sometimes has to be search, however it has an advantage over binary search in that it works on both sorted and unsorted arrays.<\/p>\n<p>&nbsp;<\/p>\n<p><iframe loading=\"lazy\" src=\"https:\/\/trinket.io\/embed\/python\/25e30e1d3b\" width=\"100%\" height=\"600\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/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;\">Time Complexity: O(log n) &#8211; Space Complexity O(1)<\/span><\/p>\n<p>Binary search is a divide and conquer searching algorithm that can <strong>only be performed on a sorted list<\/strong>.<\/p>\n<p>Each iteration through the algorithm the middle item of the array is checked to see if it is a match, it it is the index is returned, otherwise half the array is disregarded and the remaining component is searched in the same manner.<\/p>\n<p>Binary search is highly efficient in its execution, however it&#8217;s application is limited to sorted arrays. This means that in most cases the list would have to be pre-sorted first. For arrays with static components that would work well but with arrays where the values change frequently any efficiency gains are lost due to the time taken to presort the data.<\/p>\n<p>&nbsp;<\/p>\n<p><iframe loading=\"lazy\" src=\"https:\/\/trinket.io\/embed\/python\/3ebd9fd020\" width=\"100%\" height=\"600\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n\n<\/div><h2 class=\"tabtitle\">Resources<\/h2>\n<div class=\"tabcontent\">\n\n<p>Resources<\/p>\n<p><a href=\"https:\/\/www.youtube.com\/watch?v=INHF_5RIxTE\">Knight School Sorting Demonstration<\/a><\/p>\n<p><a href=\"https:\/\/www.youtube.com\/watch?v=ZZuD6iUe3Pc\">Sorting algorithms Visualised<\/a><\/p>\n<p><a href=\"https:\/\/rob-bell.net\/2009\/06\/a-beginners-guide-to-big-o-notation\/\">Big O Notation Beginner&#8217;s guide<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>What is Big O Notation? Big O notation is used used as a tool to describe the growth rate of a function in terms of the number of instructions that need to be processed (time complexity) or the amount of memory required (space complexity). This allows different algorithms to be compared in terms of their&hellip;&nbsp;<a href=\"https:\/\/learnlearn.uk\/alevelcs\/big-o-notation-searching-sorting\/\" class=\"\" rel=\"bookmark\">Read More &raquo;<span class=\"screen-reader-text\">Big O Notation with Searching &#038; Sorting<\/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":88,"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>Big O Notation with Searching &amp; Sorting - 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\/big-o-notation-searching-sorting\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Big O Notation with Searching &amp; Sorting - A Level Computer Science\" \/>\n<meta property=\"og:description\" content=\"What is Big O Notation? Big O notation is used used as a tool to describe the growth rate of a function in terms of the number of instructions that need to be processed (time complexity) or the amount of memory required (space complexity). This allows different algorithms to be compared in terms of their&hellip;&nbsp;Read More &raquo;Big O Notation with Searching &#038; Sorting\" \/>\n<meta property=\"og:url\" content=\"https:\/\/learnlearn.uk\/alevelcs\/big-o-notation-searching-sorting\/\" \/>\n<meta property=\"og:site_name\" content=\"A Level Computer Science\" \/>\n<meta property=\"article:modified_time\" content=\"2023-03-13T07:41:26+00:00\" \/>\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=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/learnlearn.uk\/alevelcs\/big-o-notation-searching-sorting\/\",\"url\":\"https:\/\/learnlearn.uk\/alevelcs\/big-o-notation-searching-sorting\/\",\"name\":\"Big O Notation with Searching & Sorting - A Level Computer Science\",\"isPartOf\":{\"@id\":\"https:\/\/learnlearn.uk\/alevelcs\/#website\"},\"datePublished\":\"2020-06-08T01:44:08+00:00\",\"dateModified\":\"2023-03-13T07:41:26+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/learnlearn.uk\/alevelcs\/big-o-notation-searching-sorting\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/learnlearn.uk\/alevelcs\/big-o-notation-searching-sorting\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/learnlearn.uk\/alevelcs\/big-o-notation-searching-sorting\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"A Level Computer Science Home\",\"item\":\"https:\/\/learnlearn.uk\/alevelcs\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Big O Notation with Searching &#038; Sorting\"}]},{\"@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":"Big O Notation with Searching & Sorting - 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\/big-o-notation-searching-sorting\/","og_locale":"en_GB","og_type":"article","og_title":"Big O Notation with Searching & Sorting - A Level Computer Science","og_description":"What is Big O Notation? Big O notation is used used as a tool to describe the growth rate of a function in terms of the number of instructions that need to be processed (time complexity) or the amount of memory required (space complexity). This allows different algorithms to be compared in terms of their&hellip;&nbsp;Read More &raquo;Big O Notation with Searching &#038; Sorting","og_url":"https:\/\/learnlearn.uk\/alevelcs\/big-o-notation-searching-sorting\/","og_site_name":"A Level Computer Science","article_modified_time":"2023-03-13T07:41:26+00:00","twitter_card":"summary_large_image","twitter_misc":{"Estimated reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/learnlearn.uk\/alevelcs\/big-o-notation-searching-sorting\/","url":"https:\/\/learnlearn.uk\/alevelcs\/big-o-notation-searching-sorting\/","name":"Big O Notation with Searching & Sorting - A Level Computer Science","isPartOf":{"@id":"https:\/\/learnlearn.uk\/alevelcs\/#website"},"datePublished":"2020-06-08T01:44:08+00:00","dateModified":"2023-03-13T07:41:26+00:00","breadcrumb":{"@id":"https:\/\/learnlearn.uk\/alevelcs\/big-o-notation-searching-sorting\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/learnlearn.uk\/alevelcs\/big-o-notation-searching-sorting\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/learnlearn.uk\/alevelcs\/big-o-notation-searching-sorting\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"A Level Computer Science Home","item":"https:\/\/learnlearn.uk\/alevelcs\/"},{"@type":"ListItem","position":2,"name":"Big O Notation with Searching &#038; Sorting"}]},{"@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 is Big O Notation? Big O notation is used used as a tool to describe the growth rate of a function in terms of the number of instructions that need to be processed (time complexity) or the amount of memory required (space complexity). This allows different algorithms to be compared in terms of their&hellip;&nbsp;Read&hellip;","_links":{"self":[{"href":"https:\/\/learnlearn.uk\/alevelcs\/wp-json\/wp\/v2\/pages\/715"}],"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=715"}],"version-history":[{"count":18,"href":"https:\/\/learnlearn.uk\/alevelcs\/wp-json\/wp\/v2\/pages\/715\/revisions"}],"predecessor-version":[{"id":1875,"href":"https:\/\/learnlearn.uk\/alevelcs\/wp-json\/wp\/v2\/pages\/715\/revisions\/1875"}],"wp:attachment":[{"href":"https:\/\/learnlearn.uk\/alevelcs\/wp-json\/wp\/v2\/media?parent=715"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}