{
    "componentChunkName": "component---src-templates-learn-js",
    "path": "/learn/ds/time_space_complex",
    "result": {"data":{"mdx":{"frontmatter":{"title":"Time and Space Complexities","date":"July 24, 2021","tags":["ds"],"description":"Time and Space Complexities"},"body":"var _excluded = [\"components\"];\n\nfunction _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }\n\nfunction _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }\n\nfunction _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }\n\n/* @jsxRuntime classic */\n\n/* @jsx mdx */\nvar _frontmatter = {\n  \"title\": \"Time and Space Complexities\",\n  \"date\": \"2021-07-24\",\n  \"description\": \"Time and Space Complexities\",\n  \"tags\": [\"ds\"],\n  \"type\": \"tuts\"\n};\n\nvar makeShortcode = function makeShortcode(name) {\n  return function MDXDefaultShortcode(props) {\n    console.warn(\"Component \" + name + \" was not imported, exported, or provided by MDXProvider as global scope\");\n    return mdx(\"div\", props);\n  };\n};\n\nvar Callouts = makeShortcode(\"Callouts\");\nvar layoutProps = {\n  _frontmatter: _frontmatter\n};\nvar MDXLayout = \"wrapper\";\nreturn function MDXContent(_ref) {\n  var components = _ref.components,\n      props = _objectWithoutProperties(_ref, _excluded);\n\n  return mdx(MDXLayout, _extends({}, layoutProps, props, {\n    components: components,\n    mdxType: \"MDXLayout\"\n  }), mdx(\"p\", null, \"Performance of an Algorithm is measured in Time and Space complexity.\"), mdx(\"ol\", null, mdx(\"li\", {\n    parentName: \"ol\"\n  }, \"Time Complexity : way of representing the amount of time needed by a program to run to the completion.\"), mdx(\"li\", {\n    parentName: \"ol\"\n  }, \"Space Complexity : amount of memory space required by an algorithm during a course of its execution. It is required in situations when limited memory is available and for the multi user system.\")), mdx(\"div\", {\n    className: \"tree-diagram\"\n  }, mdx(\"ul\", null, mdx(\"li\", null, \"Time Complexity\", mdx(\"ul\", null, mdx(\"li\", null, \"Constant Time O(1)\"), mdx(\"li\", null, \"Logarithmic time\"), mdx(\"li\", null, \"Linear Time O(n)\"), mdx(\"li\", null, \"Quadratic Time O(n2)\"), mdx(\"li\", null, \"Exponential time\"))))), mdx(\"ol\", null, mdx(\"li\", {\n    parentName: \"ol\"\n  }, \"Best Case : when the minimum amount of input is given\"), mdx(\"li\", {\n    parentName: \"ol\"\n  }, \"Worst Case : when the maximum amount of input is given.\")), mdx(Callouts, {\n    name: \"info \",\n    mdxType: \"Callouts\"\n  }, \"During analyses of algorithm, mostly we will consider worst case scenarios only\"), mdx(Callouts, {\n    name: \"warning \",\n    mdxType: \"Callouts\"\n  }, \"Assigning values to variables or return statement or print statements takes constant time 1 unit represented in O(1) , it won't be dependent on length of array or any other .\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-js\",\n    \"metastring\": \"file=index.js highlights=13 show=false\",\n    \"file\": \"index.js\",\n    \"highlights\": \"13\",\n    \"show\": \"false\"\n  }, \"int a = 5;      // 1\\n\\nfor(int i=0;i<n;i++)   //n + 1  (n times loop runs, 1 time checks condition and fails)\\n  System.out.println(i);  // 1\\n----------------------------------\\nTime Complexity :\\nf(n) = 1 + (n+1) +1\\nf(n) = n+3\\nf(n) = n\\n\\n= O(n)\\n\\n\")), mdx(\"h4\", null, \"Rules of Time Complexity\"), mdx(\"ul\", null, mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"*\", \" Find the fastest growing term\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"*\", \" Take out the coefficent\")), mdx(\"ol\", {\n    \"start\": 2\n  }, mdx(\"li\", {\n    parentName: \"ol\"\n  }, \"Drop Constants\")), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-js\",\n    \"metastring\": \"file=index.js highlights=13 show=false\",\n    \"file\": \"index.js\",\n    \"highlights\": \"13\",\n    \"show\": \"false\"\n  }, \"{\\n  for() { ... } // O(n)\\n  for() { ... } // O(n)\\n}\\n----------------------------------\\nTime Complexity :\\n= O(n) + O(n)\\n= O( \\u03362\\u0336n) //dropping constants,2 is deleted\\n= O(n)\\n\\n= O(n)\\n\\n\")), mdx(\"ol\", {\n    \"start\": 3\n  }, mdx(\"li\", {\n    parentName: \"ol\"\n  }, \"Drop non-dominate term / growing term\")), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-js\",\n    \"metastring\": \"file=index.js highlights=13 show=false\",\n    \"file\": \"index.js\",\n    \"highlights\": \"13\",\n    \"show\": \"false\"\n  }, \"\\nf(n) = O(n) + O(n\\xB2)  // O(n\\xB2) is fast growing, O(n) is non dominate\\n= O(n\\xB2)\\n\\n\")), mdx(\"ol\", {\n    \"start\": 3\n  }, mdx(\"li\", {\n    parentName: \"ol\"\n  }, \"Different inputs => Different variables (Nested Loops)\")), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-js\",\n    \"metastring\": \"file=index.js highlights=13 show=false\",\n    \"file\": \"index.js\",\n    \"highlights\": \"13\",\n    \"show\": \"false\"\n  }, \"\\nfor ( int i =0;i<a;i++) //O(n)\\n{\\n  for ( int j=0;j<b;j++) { } //O(n)\\n}\\n\\nTime Complexity\\nf(n)= O(a * b) // O(length of arrray a  * length of array b)\\nf(n) = O(n * n )\\nf(n) = O(n\\xB2 )\\n\")), mdx(\"h4\", null, \"Time Complexities in Order :\"), mdx(\"p\", null, \"O(1) < O(log n) < O(n) O(n log n) < O(n\\xB2) < O(2\\u207F) < O(n!)\"), mdx(\"p\", null, mdx(\"span\", {\n    parentName: \"p\",\n    \"className\": \"gatsby-resp-image-wrapper\",\n    \"style\": {\n      \"position\": \"relative\",\n      \"display\": \"block\",\n      \"marginLeft\": \"auto\",\n      \"marginRight\": \"auto\",\n      \"maxWidth\": \"1200px\"\n    }\n  }, \"\\n      \", mdx(\"a\", {\n    parentName: \"span\",\n    \"className\": \"gatsby-resp-image-link\",\n    \"href\": \"/static/0dcd373fcd59edcce4199d30401e55a3/d165a/time_complex.jpg\",\n    \"style\": {\n      \"display\": \"block\"\n    },\n    \"target\": \"_blank\",\n    \"rel\": \"noopener\"\n  }, \"\\n    \", mdx(\"span\", {\n    parentName: \"a\",\n    \"className\": \"gatsby-resp-image-background-image\",\n    \"style\": {\n      \"paddingBottom\": \"69.66666666666667%\",\n      \"position\": \"relative\",\n      \"bottom\": \"0\",\n      \"left\": \"0\",\n      \"backgroundImage\": \"url('data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAOABQDASIAAhEBAxEB/8QAGAAAAgMAAAAAAAAAAAAAAAAAAAECBAX/xAAVAQEBAAAAAAAAAAAAAAAAAAADBP/aAAwDAQACEAMQAAAB0U5TPYEKP//EABsQAAICAwEAAAAAAAAAAAAAAAECABEDEiEx/9oACAEBAAEFAjkawzRDY07E8//EABcRAQADAAAAAAAAAAAAAAAAAAABAhH/2gAIAQMBAT8B2qX/xAAZEQEAAgMAAAAAAAAAAAAAAAABAAIDETH/2gAIAQIBAT8BvkTkoup//8QAFRABAQAAAAAAAAAAAAAAAAAAARD/2gAIAQEABj8Ca3//xAAaEAADAAMBAAAAAAAAAAAAAAAAAREhMVGx/9oACAEBAAE/IZC4vCupjavJtObEqep//9oADAMBAAIAAwAAABBs3//EABgRAQEAAwAAAAAAAAAAAAAAAAEAITGh/9oACAEDAQE/EETXJC4v/8QAFxEBAQEBAAAAAAAAAAAAAAAAAQAxEf/aAAgBAgEBPxBDmpBXb//EABsQAAIDAAMAAAAAAAAAAAAAAAERACExQWFx/9oACAEBAAE/ECwRsApkrClpFQmKzTSjcwNDpwYY0VqgoqXpz//Z')\",\n      \"backgroundSize\": \"cover\",\n      \"display\": \"block\"\n    }\n  }), \"\\n  \", mdx(\"img\", {\n    parentName: \"a\",\n    \"className\": \"gatsby-resp-image-image\",\n    \"alt\": \"How css Works Image\",\n    \"title\": \"css works\",\n    \"src\": \"/static/0dcd373fcd59edcce4199d30401e55a3/e5166/time_complex.jpg\",\n    \"srcSet\": [\"/static/0dcd373fcd59edcce4199d30401e55a3/f93b5/time_complex.jpg 300w\", \"/static/0dcd373fcd59edcce4199d30401e55a3/b4294/time_complex.jpg 600w\", \"/static/0dcd373fcd59edcce4199d30401e55a3/e5166/time_complex.jpg 1200w\", \"/static/0dcd373fcd59edcce4199d30401e55a3/d165a/time_complex.jpg 1400w\"],\n    \"sizes\": \"(max-width: 1200px) 100vw, 1200px\",\n    \"style\": {\n      \"width\": \"100%\",\n      \"height\": \"100%\",\n      \"margin\": \"0\",\n      \"verticalAlign\": \"middle\",\n      \"position\": \"absolute\",\n      \"top\": \"0\",\n      \"left\": \"0\"\n    },\n    \"loading\": \"lazy\",\n    \"decoding\": \"async\"\n  }), \"\\n  \"), \"\\n    \")), mdx(\"p\", null, mdx(\"a\", {\n    parentName: \"p\",\n    \"href\": \"https://www.bigocheatsheet.com/\"\n  }, \"image Source\")));\n}\n;\nMDXContent.isMDXComponent = true;","slug":"ds/time_space_complex"},"allMdx":{"edges":[{"node":{"slug":"ds/arrays","id":"8ab43cbe-bc50-5590-925e-1a45d7028386"}},{"node":{"slug":"ds/binary_addition","id":"26e70246-f787-5a17-baa8-9375bbacc879"}},{"node":{"slug":"ds/binary_divison","id":"ec6f892f-531e-5661-8a44-d08f906ecba5"}},{"node":{"slug":"ds/binary_operations","id":"b2e4a072-3108-546d-a06a-646e43d834cc"}},{"node":{"slug":"ds/binary_multiplication","id":"0b241fae-5107-5843-b21e-7a5ed393168f"}},{"node":{"slug":"ds/binary_substraction","id":"18f53266-039e-5e0c-af57-37c05a2e80ec"}},{"node":{"slug":"ds/linear_data_structures","id":"0d96c16b-7430-59aa-b81a-73e67dcfd3d2"}},{"node":{"slug":"ds/bit_manipulation","id":"03ba2551-8800-5f50-9724-3a84d09a5989"}},{"node":{"slug":"ds/intro","id":"67e2c78b-07c3-57cf-9984-08e405f77783"}},{"node":{"slug":"ds/roadmap","id":"f7856a33-93ca-5464-8e5a-d5dc41765650"}},{"node":{"slug":"ds/programming_language","id":"ac14fda6-ff3a-5306-964a-3ce25168a6f0"}},{"node":{"slug":"ds/number_systems","id":"12494952-aaee-5db2-91ef-4db461c5b95c"}},{"node":{"slug":"ds/time_space_complex","id":"b7695373-5e9c-56c5-9b7f-5a632785916f"}}]}},"pageContext":{"id":"b7695373-5e9c-56c5-9b7f-5a632785916f","filePath":"/ds/","previousPostId":"ds/programming_language","nextPostId":"js/intro"}},
    "staticQueryHashes": ["2033233798"]}