{"id":551,"date":"2023-03-23T07:37:49","date_gmt":"2023-03-23T07:37:49","guid":{"rendered":"http:\/\/learnlearn.uk\/ibcs\/?page_id=551"},"modified":"2025-04-05T11:10:22","modified_gmt":"2025-04-05T11:10:22","slug":"big-o-notation","status":"publish","type":"page","link":"https:\/\/learnlearn.uk\/ibcs\/big-o-notation\/","title":{"rendered":"Big O Notation"},"content":{"rendered":"<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">What is Big O Notation?<\/h2>\n<div class=\"tabcontent\">\n\n<h3>What is Big O Notation?<\/h3>\n<p>Big O Notation is a mathematical concept used in computer science to describe the **performance or complexity** of an algorithm. It expresses **how the execution time or memory usage grows** relative to the size of the input.<\/p>\n<p>Big O focuses on the **worst-case scenario**, helping developers ensure algorithms will perform reliably even with large data sets.<\/p>\n<h4>Why It Matters<\/h4>\n<p>Understanding Big O is crucial for writing efficient code. Here&#8217;s why:<\/p>\n<p>&#8211; <strong>Efficiency:<\/strong> Better algorithms lead to faster, more efficient programs.<br \/>\n&#8211; <strong>Scalability:<\/strong> Big O helps predict how code behaves with growing input sizes.<br \/>\n&#8211; <strong>Optimization:<\/strong> Identifies bottlenecks in time and space usage.<br \/>\n&#8211; <strong>Technical Interviews:<\/strong> A key topic in coding interviews and assessments.<\/p>\n\n<\/div><h2 class=\"tabtitle\">Common Notations<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Common Big O Notations<\/h3>\n<table>\n<thead>\n<tr>\n<th>Big O<\/th>\n<th>Name<\/th>\n<th>Example<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>O(1)<\/td>\n<td>Constant Time<\/td>\n<td>Accessing an element in an array<\/td>\n<\/tr>\n<tr>\n<td>O(log n)<\/td>\n<td>Logarithmic Time<\/td>\n<td>Binary search<\/td>\n<\/tr>\n<tr>\n<td>O(n)<\/td>\n<td>Linear Time<\/td>\n<td>Looping through an array<\/td>\n<\/tr>\n<tr>\n<td>O(n log n)<\/td>\n<td>Linearithmic Time<\/td>\n<td>Merge sort, quicksort<\/td>\n<\/tr>\n<tr>\n<td>O(n\u00b2)<\/td>\n<td>Quadratic Time<\/td>\n<td>Nested loops (e.g., bubble sort)<\/td>\n<\/tr>\n<tr>\n<td>O(2\u207f)<\/td>\n<td>Exponential Time<\/td>\n<td>Recursive algorithms (e.g., Fibonacci)<\/td>\n<\/tr>\n<tr>\n<td>O(n!)<\/td>\n<td>Factorial Time<\/td>\n<td>Brute-force permutations<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n\n<\/div><h2 class=\"tabtitle\">Examples<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Examples<\/h3>\n<pre><strong>1. O(1) \u2013 Constant Time<\/strong>\r\n\r\ndef get_first_element(arr):\r\n    return arr[0]\r\n    This function returns the first element regardless of input size.<\/pre>\n<p><strong>2. O(n) \u2013 Linear Time<\/strong><\/p>\n<pre>def print_all_elements(arr):\r\n\u00a0 \u00a0 for item in arr:\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0print(item)<\/pre>\n<p>The number of operations grows linearly with the input.<\/p>\n<p><strong>3. O(n\u00b2) \u2013 Quadratic Time<\/strong><\/p>\n<pre>def print_pairs(arr):\r\n\u00a0 \u00a0 for i in arr:\r\n\u00a0 \u00a0 \u00a0 \u00a0 for j in arr:\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 print(i, j)<\/pre>\n<p>Nested loops cause the time to grow quadratically<\/p>\n\n<\/div><h2 class=\"tabtitle\">Space Complexity<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Space Complexity<\/h3>\n<p>Big O is also used to describe **space complexity**, or how much memory an algorithm uses.<br \/>\n<strong>O(1):<\/strong> Constant space, memory usage doesn&#8217;t grow with input.<\/p>\n<p><strong>O(n):<\/strong> Linear space, memory usage grows with input size (e.g., storing results in a new array).<\/p>\n<p><strong>O(n\u00b2):<\/strong> Common in matrix-based algorithms or recursive calls with large depth.<\/p>\n<p>Efficient memory usage is just as important as fast execution in large-scale systems<\/p>\n\n<\/div><h2 class=\"tabtitle\">Tips for Analyzing Code<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Tips for Analyzing Code<\/h3>\n<p>&#8211; Ignore constants: O(2n) simplifies to O(n)<\/p>\n<p>&#8211; Drop smaller terms: O(n\u00b2 + n) simplifies to O(n\u00b2)<\/p>\n<p>&#8211; One loop = O(n), nested loops = O(n\u00b2), triple loops = O(n\u00b3)<\/p>\n<p>&#8211; Recursive functions often follow patterns like T(n) = T(n\/2) + O(1)<\/p>\n<p>&nbsp;<\/p>\n\n<\/div><h2 class=\"tabtitle\">Graph<\/h2>\n<div class=\"tabcontent\">\n\n<p>&nbsp;<\/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<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 a mathematical concept used in computer science to describe the **performance or complexity** of an algorithm. It expresses **how the execution time or memory usage grows** relative to the size of the input. Big O focuses on the **worst-case scenario**, helping developers ensure algorithms will perform&hellip;&nbsp;<a href=\"https:\/\/learnlearn.uk\/ibcs\/big-o-notation\/\" class=\"\" rel=\"bookmark\">Read More &raquo;<span class=\"screen-reader-text\">Big O Notation<\/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":70,"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 - IB 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\/ibcs\/big-o-notation\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Big O Notation - IB Computer Science\" \/>\n<meta property=\"og:description\" content=\"What is Big O Notation? Big O Notation is a mathematical concept used in computer science to describe the **performance or complexity** of an algorithm. It expresses **how the execution time or memory usage grows** relative to the size of the input. Big O focuses on the **worst-case scenario**, helping developers ensure algorithms will perform&hellip;&nbsp;Read More &raquo;Big O Notation\" \/>\n<meta property=\"og:url\" content=\"https:\/\/learnlearn.uk\/ibcs\/big-o-notation\/\" \/>\n<meta property=\"og:site_name\" content=\"IB Computer Science\" \/>\n<meta property=\"article:modified_time\" content=\"2025-04-05T11:10:22+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=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/learnlearn.uk\/ibcs\/big-o-notation\/\",\"url\":\"https:\/\/learnlearn.uk\/ibcs\/big-o-notation\/\",\"name\":\"Big O Notation - IB Computer Science\",\"isPartOf\":{\"@id\":\"https:\/\/learnlearn.uk\/ibcs\/#website\"},\"datePublished\":\"2023-03-23T07:37:49+00:00\",\"dateModified\":\"2025-04-05T11:10:22+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/learnlearn.uk\/ibcs\/big-o-notation\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/learnlearn.uk\/ibcs\/big-o-notation\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/learnlearn.uk\/ibcs\/big-o-notation\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"IB Computer Science\",\"item\":\"https:\/\/learnlearn.uk\/ibcs\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Big O Notation\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/learnlearn.uk\/ibcs\/#website\",\"url\":\"https:\/\/learnlearn.uk\/ibcs\/\",\"name\":\"IB Computer Science\",\"description\":\"- learnlearn..uk\",\"publisher\":{\"@id\":\"https:\/\/learnlearn.uk\/ibcs\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/learnlearn.uk\/ibcs\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/learnlearn.uk\/ibcs\/#organization\",\"name\":\"IB Computer Science\",\"url\":\"https:\/\/learnlearn.uk\/ibcs\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\/\/learnlearn.uk\/ibcs\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/learnlearn.uk\/ibcs\/wp-content\/uploads\/sites\/25\/2022\/09\/LearnLearnLogowhite-300x41.png\",\"contentUrl\":\"https:\/\/learnlearn.uk\/ibcs\/wp-content\/uploads\/sites\/25\/2022\/09\/LearnLearnLogowhite-300x41.png\",\"width\":300,\"height\":41,\"caption\":\"IB Computer Science\"},\"image\":{\"@id\":\"https:\/\/learnlearn.uk\/ibcs\/#\/schema\/logo\/image\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Big O Notation - IB 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\/ibcs\/big-o-notation\/","og_locale":"en_GB","og_type":"article","og_title":"Big O Notation - IB Computer Science","og_description":"What is Big O Notation? Big O Notation is a mathematical concept used in computer science to describe the **performance or complexity** of an algorithm. It expresses **how the execution time or memory usage grows** relative to the size of the input. Big O focuses on the **worst-case scenario**, helping developers ensure algorithms will perform&hellip;&nbsp;Read More &raquo;Big O Notation","og_url":"https:\/\/learnlearn.uk\/ibcs\/big-o-notation\/","og_site_name":"IB Computer Science","article_modified_time":"2025-04-05T11:10:22+00:00","twitter_card":"summary_large_image","twitter_misc":{"Estimated reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/learnlearn.uk\/ibcs\/big-o-notation\/","url":"https:\/\/learnlearn.uk\/ibcs\/big-o-notation\/","name":"Big O Notation - IB Computer Science","isPartOf":{"@id":"https:\/\/learnlearn.uk\/ibcs\/#website"},"datePublished":"2023-03-23T07:37:49+00:00","dateModified":"2025-04-05T11:10:22+00:00","breadcrumb":{"@id":"https:\/\/learnlearn.uk\/ibcs\/big-o-notation\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/learnlearn.uk\/ibcs\/big-o-notation\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/learnlearn.uk\/ibcs\/big-o-notation\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"IB Computer Science","item":"https:\/\/learnlearn.uk\/ibcs\/"},{"@type":"ListItem","position":2,"name":"Big O Notation"}]},{"@type":"WebSite","@id":"https:\/\/learnlearn.uk\/ibcs\/#website","url":"https:\/\/learnlearn.uk\/ibcs\/","name":"IB Computer Science","description":"- learnlearn..uk","publisher":{"@id":"https:\/\/learnlearn.uk\/ibcs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/learnlearn.uk\/ibcs\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-GB"},{"@type":"Organization","@id":"https:\/\/learnlearn.uk\/ibcs\/#organization","name":"IB Computer Science","url":"https:\/\/learnlearn.uk\/ibcs\/","logo":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/learnlearn.uk\/ibcs\/#\/schema\/logo\/image\/","url":"https:\/\/learnlearn.uk\/ibcs\/wp-content\/uploads\/sites\/25\/2022\/09\/LearnLearnLogowhite-300x41.png","contentUrl":"https:\/\/learnlearn.uk\/ibcs\/wp-content\/uploads\/sites\/25\/2022\/09\/LearnLearnLogowhite-300x41.png","width":300,"height":41,"caption":"IB Computer Science"},"image":{"@id":"https:\/\/learnlearn.uk\/ibcs\/#\/schema\/logo\/image\/"}}]}},"rttpg_featured_image_url":null,"rttpg_author":{"display_name":"learnlearnadmin","author_link":"https:\/\/learnlearn.uk\/ibcs\/author\/learnlearnadmin\/"},"rttpg_comment":0,"rttpg_category":null,"rttpg_excerpt":"What is Big O Notation? Big O Notation is a mathematical concept used in computer science to describe the **performance or complexity** of an algorithm. It expresses **how the execution time or memory usage grows** relative to the size of the input. Big O focuses on the **worst-case scenario**, helping developers ensure algorithms will perform&hellip;&nbsp;Read&hellip;","_links":{"self":[{"href":"https:\/\/learnlearn.uk\/ibcs\/wp-json\/wp\/v2\/pages\/551"}],"collection":[{"href":"https:\/\/learnlearn.uk\/ibcs\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/learnlearn.uk\/ibcs\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/learnlearn.uk\/ibcs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnlearn.uk\/ibcs\/wp-json\/wp\/v2\/comments?post=551"}],"version-history":[{"count":5,"href":"https:\/\/learnlearn.uk\/ibcs\/wp-json\/wp\/v2\/pages\/551\/revisions"}],"predecessor-version":[{"id":1205,"href":"https:\/\/learnlearn.uk\/ibcs\/wp-json\/wp\/v2\/pages\/551\/revisions\/1205"}],"wp:attachment":[{"href":"https:\/\/learnlearn.uk\/ibcs\/wp-json\/wp\/v2\/media?parent=551"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}