{"id":1939,"date":"2024-07-01T10:38:07","date_gmt":"2024-07-01T10:38:07","guid":{"rendered":"https:\/\/cppdepend.com\/blog\/?p=1939"},"modified":"2024-07-09T10:46:24","modified_gmt":"2024-07-09T10:46:24","slug":"7-tips-to-improve-the-c-algorithms-complexity-big-o","status":"publish","type":"post","link":"https:\/\/cppdepend.com\/blog\/7-tips-to-improve-the-c-algorithms-complexity-big-o\/","title":{"rendered":"7 Tips to improve the C++ algorithms complexity (Big-O)"},"content":{"rendered":"\n<p>Understanding algorithm complexity, specifically Big-O notation, is crucial for analyzing and comparing the efficiency of algorithms. In C++, as in other programming languages, the complexity of an algorithm can significantly impact the performance and scalability of an application.  So it&#8217;s useful to know how to improve the algorithm complexity. But before that let&#8217;s explore the most common algorithm complexities:<\/p>\n\n\n\n<!--more-->\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>O(1) &#8211; Constant Time<\/strong><\/h3>\n\n\n\n<p><strong>Example:<\/strong> Accessing an element in an array by index.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:false,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">#include &lt;iostream&gt;\n\nint main() {\n    int arr[] = {1, 2, 3, 4, 5};\n    int index = 2;\n    std::cout &lt;&lt; &quot;Element at index &quot; &lt;&lt; index &lt;&lt; &quot; is &quot; &lt;&lt; arr[index] &lt;&lt; std::endl;  \/\/ O(1) operation\n    return 0;\n}<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>O(log n) &#8211; Logarithmic Time<\/strong><\/h3>\n\n\n\n<p><strong>Example:<\/strong> Binary search in a sorted array.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:false,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">#include &lt;iostream&gt;\n#include &lt;vector&gt;\n#include &lt;algorithm&gt;\n\nint binarySearch(const std::vector&lt;int&gt;&amp; arr, int target) {\n    int left = 0, right = arr.size() - 1;\n    while (left &lt;= right) {\n        int mid = left + (right - left) \/ 2;\n        if (arr[mid] == target) return mid;\n        if (arr[mid] &lt; target) left = mid + 1;\n        else right = mid - 1;\n    }\n    return -1;\n}\n\nint main() {\n    std::vector&lt;int&gt; arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};\n    int target = 7;\n    int result = binarySearch(arr, target);\n    if (result != -1)\n        std::cout &lt;&lt; &quot;Element found at index &quot; &lt;&lt; result &lt;&lt; std::endl;\n    else\n        std::cout &lt;&lt; &quot;Element not found&quot; &lt;&lt; std::endl;\n    return 0;\n}<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>O(n) &#8211; Linear Time<\/strong><\/h3>\n\n\n\n<p><strong>Example:<\/strong> Summing the elements of an array.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:false,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">#include &lt;iostream&gt;\n\nint sumArray(const int arr[], int size) {\n    int sum = 0;\n    for (int i = 0; i &lt; size; ++i) {\n        sum += arr[i];\n    }\n    return sum;\n}\n\nint main() {\n    int arr[] = {1, 2, 3, 4, 5};\n    int size = sizeof(arr) \/ sizeof(arr[0]);\n    std::cout &lt;&lt; &quot;Sum of array elements is &quot; &lt;&lt; sumArray(arr, size) &lt;&lt; std::endl;\n    return 0;\n}<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>O(n log n) &#8211; Linearithmic Time<\/strong><\/h3>\n\n\n\n<p><strong>Example:<\/strong> Mergesort algorithm.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:false,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">#include &lt;iostream&gt;\n#include &lt;vector&gt;\n\nvoid merge(std::vector&lt;int&gt;&amp; arr, int left, int mid, int right) {\n    int n1 = mid - left + 1;\n    int n2 = right - mid;\n    std::vector&lt;int&gt; L(n1), R(n2);\n\n    for (int i = 0; i &lt; n1; ++i)\n        L[i] = arr[left + i];\n    for (int i = 0; i &lt; n2; ++i)\n        R[i] = arr[mid + 1 + i];\n\n    int i = 0, j = 0, k = left;\n    while (i &lt; n1 &amp;&amp; j &lt; n2) {\n        if (L[i] &lt;= R[j]) {\n            arr[k++] = L[i++];\n        } else {\n            arr[k++] = R[j++];\n        }\n    }\n    while (i &lt; n1) arr[k++] = L[i++];\n    while (j &lt; n2) arr[k++] = R[j++];\n}\n\nvoid mergeSort(std::vector&lt;int&gt;&amp; arr, int left, int right) {\n    if (left &lt; right) {\n        int mid = left + (right - left) \/ 2;\n        mergeSort(arr, left, mid);\n        mergeSort(arr, mid + 1, right);\n        merge(arr, left, mid, right);\n    }\n}\n\nint main() {\n    std::vector&lt;int&gt; arr = {12, 11, 13, 5, 6, 7};\n    mergeSort(arr, 0, arr.size() - 1);\n    for (int x : arr) std::cout &lt;&lt; x &lt;&lt; &quot; &quot;;\n    std::cout &lt;&lt; std::endl;\n    return 0;\n}<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong>O(n^2) &#8211; Quadratic Time<\/strong><\/h3>\n\n\n\n<p><strong>Example:<\/strong> Bubble sort algorithm.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:false,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">#include &lt;iostream&gt;\n\nvoid bubbleSort(int arr[], int size) {\n    for (int i = 0; i &lt; size - 1; ++i) {\n        for (int j = 0; j &lt; size - i - 1; ++j) {\n            if (arr[j] &gt; arr[j + 1]) {\n                std::swap(arr[j], arr[j + 1]);\n            }\n        }\n    }\n}\n\nint main() {\n    int arr[] = {5, 1, 4, 2, 8};\n    int size = sizeof(arr) \/ sizeof(arr[0]);\n    bubbleSort(arr, size);\n    for (int i = 0; i &lt; size; ++i)\n        std::cout &lt;&lt; arr[i] &lt;&lt; &quot; &quot;;\n    std::cout &lt;&lt; std::endl;\n    return 0;\n}<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">6. <strong>O(n^3) &#8211; Cubic Time<\/strong><\/h3>\n\n\n\n<p><strong>Example:<\/strong> Checking if three numbers in an array sum up to a given number.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:false,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">#include &lt;iostream&gt;\n\nbool findTriplet(int arr[], int n, int sum) {\n    for (int i = 0; i &lt; n - 2; ++i) {\n        for (int j = i + 1; j &lt; n - 1; ++j) {\n            for (int k = j + 1; k &lt; n; ++k) {\n                if (arr[i] + arr[j] + arr[k] == sum) {\n                    std::cout &lt;&lt; &quot;Triplet found: &quot; &lt;&lt; arr[i] &lt;&lt; &quot;, &quot; &lt;&lt; arr[j] &lt;&lt; &quot;, &quot; &lt;&lt; arr[k] &lt;&lt; std::endl;\n                    return true;\n                }\n            }\n        }\n    }\n    return false;\n}\n\nint main() {\n    int arr[] = {1, 4, 45, 6, 10, 8};\n    int sum = 22;\n    int size = sizeof(arr) \/ sizeof(arr[0]);\n    if (!findTriplet(arr, size, sum))\n        std::cout &lt;&lt; &quot;No triplet found&quot; &lt;&lt; std::endl;\n    return 0;\n}<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">7. <strong>O(2^n) &#8211; Exponential Time<\/strong><\/h3>\n\n\n\n<p><strong>Example:<\/strong> Recursive calculation of Fibonacci numbers.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:false,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">#include &lt;iostream&gt;\n\nint fibonacci(int n) {\n    if (n &lt;= 1) return n;\n    return fibonacci(n - 1) + fibonacci(n - 2);\n}\n\nint main() {\n    int n = 5;\n    std::cout &lt;&lt; &quot;Fibonacci of &quot; &lt;&lt; n &lt;&lt; &quot; is &quot; &lt;&lt; fibonacci(n) &lt;&lt; std::endl;\n    return 0;\n}<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">8. <strong>O(n!) &#8211; Factorial Time<\/strong><\/h3>\n\n\n\n<p><strong>Example:<\/strong> Generating all permutations of a string.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:false,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">#include &lt;iostream&gt;\n#include &lt;string&gt;\n#include &lt;algorithm&gt;\n\nvoid permute(std::string str, int l, int r) {\n    if (l == r) {\n        std::cout &lt;&lt; str &lt;&lt; std::endl;\n    } else {\n        for (int i = l; i &lt;= r; ++i) {\n            std::swap(str[l], str[i]);\n            permute(str, l + 1, r);\n            std::swap(str[l], str[i]); \/\/ backtrack\n        }\n    }\n}\n\nint main() {\n    std::string str = &quot;ABC&quot;;\n    permute(str, 0, str.size() - 1);\n    return 0;\n}<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-tips-to-optimize-the-big-o\">Tips to optimize the Big-O<\/h3>\n\n\n\n<p>Optimizing the algorithm complexity often involves using different techniques to reduce the time or space complexity of an algorithm. Here are some common techniques to optimize algorithm complexity:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>Choosing the Right Data Structures<\/strong><\/h3>\n\n\n\n<p>Choosing the right data structures is crucial for optimizing the Big O complexity of your algorithms. Different data structures provide different efficiencies for various operations such as insertion, deletion, access, and search. Below, I&#8217;ll discuss several common data structures, their typical operations, and provide C++ examples for each.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-1-1-arrays\">1. 1 <strong>Arrays<\/strong><\/h3>\n\n\n\n<p><strong>Properties:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Fixed size<\/li>\n\n\n\n<li>Contiguous memory allocation<\/li>\n\n\n\n<li>Fast access by index (O(1))<\/li>\n\n\n\n<li>Slow insertions and deletions (O(n))<\/li>\n<\/ul>\n\n\n\n<p><strong>Use Cases:<\/strong> When the number of elements is known in advance and fast access by index is required.<\/p>\n\n\n\n<p><strong>C++ Example:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:false,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">#include &lt;iostream&gt;\n\nint main() {\n    int arr[] = {1, 2, 3, 4, 5};\n    int index = 2;\n    std::cout &lt;&lt; &quot;Element at index &quot; &lt;&lt; index &lt;&lt; &quot; is &quot; &lt;&lt; arr[index] &lt;&lt; std::endl; \/\/ O(1) access\n    return 0;\n}<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-1-2-linked-lists\">1.2. <strong>Linked Lists<\/strong><\/h3>\n\n\n\n<p><strong>Properties:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Dynamic size<\/li>\n\n\n\n<li>Non-contiguous memory allocation<\/li>\n\n\n\n<li>Fast insertions and deletions (O(1) if the position is known)<\/li>\n\n\n\n<li>Slow access by index (O(n))<\/li>\n<\/ul>\n\n\n\n<p><strong>Use Cases:<\/strong> When the number of elements is unknown or variable, and frequent insertions\/deletions are required.<\/p>\n\n\n\n<p><strong>C++ Example:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:false,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">#include &lt;iostream&gt;\n\nstruct Node {\n    int data;\n    Node* next;\n};\n\nvoid append(Node*&amp; head, int new_data) {\n    Node* new_node = new Node();\n    new_node-&gt;data = new_data;\n    new_node-&gt;next = nullptr;\n    if (!head) {\n        head = new_node;\n        return;\n    }\n    Node* last = head;\n    while (last-&gt;next) {\n        last = last-&gt;next;\n    }\n    last-&gt;next = new_node;\n}\n\nvoid printList(Node* node) {\n    while (node) {\n        std::cout &lt;&lt; node-&gt;data &lt;&lt; &quot; &quot;;\n        node = node-&gt;next;\n    }\n    std::cout &lt;&lt; std::endl;\n}\n\nint main() {\n    Node* head = nullptr;\n    append(head, 1);\n    append(head, 2);\n    append(head, 3);\n    printList(head); \/\/ O(n) traversal\n    return 0;\n}<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-1-3-hash-tables-unordered-map\">1.3. <strong>Hash Tables (unordered_map)<\/strong><\/h3>\n\n\n\n<p><strong>Properties:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Average O(1) time complexity for insertions, deletions, and access<\/li>\n\n\n\n<li>Dynamic size<\/li>\n\n\n\n<li>Non-contiguous memory allocation<\/li>\n<\/ul>\n\n\n\n<p><strong>Use Cases:<\/strong> When fast access, insertion, and deletion are required, and the order of elements is not important.<\/p>\n\n\n\n<p><strong>C++ Example:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:false,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">#include &lt;iostream&gt;\n#include &lt;unordered_map&gt;\n\nint main() {\n    std::unordered_map&lt;std::string, int&gt; umap;\n    umap[&quot;one&quot;] = 1;\n    umap[&quot;two&quot;] = 2;\n    umap[&quot;three&quot;] = 3;\n\n    std::cout &lt;&lt; &quot;Value for key 'two': &quot; &lt;&lt; umap[&quot;two&quot;] &lt;&lt; std::endl; \/\/ O(1) access\n    return 0;\n}<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-1-4-balanced-trees-e-g-std-map-std-set\">1.4. <strong>Balanced Trees (e.g., std::map, std::set)<\/strong><\/h3>\n\n\n\n<p><strong>Properties:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>O(log n) time complexity for insertions, deletions, and access<\/li>\n\n\n\n<li>Elements are kept in sorted order<\/li>\n<\/ul>\n\n\n\n<p><strong>Use Cases:<\/strong> When sorted order of elements is required and relatively fast insertions, deletions, and access are needed.<\/p>\n\n\n\n<p><strong>C++ Example:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:false,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">#include &lt;iostream&gt;\n#include &lt;map&gt;\n\nint main() {\n    std::map&lt;std::string, int&gt; omap;\n    omap[&quot;one&quot;] = 1;\n    omap[&quot;two&quot;] = 2;\n    omap[&quot;three&quot;] = 3;\n\n    std::cout &lt;&lt; &quot;Value for key 'two': &quot; &lt;&lt; omap[&quot;two&quot;] &lt;&lt; std::endl; \/\/ O(log n) access\n    for (const auto&amp; pair : omap) {\n        std::cout &lt;&lt; pair.first &lt;&lt; &quot;: &quot; &lt;&lt; pair.second &lt;&lt; std::endl;\n    }\n    return 0;\n}<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-1-5-heaps-priority-queue\">1.5. <strong>Heaps (Priority Queue)<\/strong><\/h3>\n\n\n\n<p><strong>Properties:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>O(log n) time complexity for insertions and deletions<\/li>\n\n\n\n<li>O(1) time complexity for access to the maximum\/minimum element<\/li>\n<\/ul>\n\n\n\n<p><strong>Use Cases:<\/strong> When a dynamically changing dataset is needed and you frequently need to access the largest or smallest element.<\/p>\n\n\n\n<p><strong>C++ Example:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:false,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">#include &lt;iostream&gt;\n#include &lt;queue&gt;\n#include &lt;vector&gt;\n\nint main() {\n    std::priority_queue&lt;int&gt; pq;\n    pq.push(10);\n    pq.push(30);\n    pq.push(20);\n\n    std::cout &lt;&lt; &quot;Top element is &quot; &lt;&lt; pq.top() &lt;&lt; std::endl; \/\/ O(1) access to the maximum element\n\n    pq.pop(); \/\/ O(log n) deletion\n    std::cout &lt;&lt; &quot;Top element after pop is &quot; &lt;&lt; pq.top() &lt;&lt; std::endl;\n    return 0;\n}<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-1-6-graphs\">1.6. <strong>Graphs<\/strong><\/h3>\n\n\n\n<p><strong>Properties:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Represented as adjacency lists or adjacency matrices<\/li>\n\n\n\n<li>Adjacency list: O(V + E) for traversal<\/li>\n\n\n\n<li>Adjacency matrix: O(V^2) for traversal<\/li>\n<\/ul>\n\n\n\n<p><strong>Use Cases:<\/strong> When representing relationships or connections between entities, such as social networks or transportation networks.<\/p>\n\n\n\n<p><strong>C++ Example (Adjacency List):<\/strong><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:false,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">#include &lt;iostream&gt;\n#include &lt;vector&gt;\n\nvoid addEdge(std::vector&lt;int&gt; adj[], int u, int v) {\n    adj[u].push_back(v);\n    adj[v].push_back(u); \/\/ For undirected graph\n}\n\nvoid printGraph(const std::vector&lt;int&gt; adj[], int V) {\n    for (int v = 0; v &lt; V; ++v) {\n        std::cout &lt;&lt; &quot;Adjacency list of vertex &quot; &lt;&lt; v &lt;&lt; &quot;\\n head &quot;;\n        for (auto x : adj[v]) std::cout &lt;&lt; &quot;-&gt; &quot; &lt;&lt; x;\n        std::cout &lt;&lt; std::endl;\n    }\n}\n\nint main() {\n    int V = 5;\n    std::vector&lt;int&gt; adj[V];\n    addEdge(adj, 0, 1);\n    addEdge(adj, 0, 4);\n    addEdge(adj, 1, 2);\n    addEdge(adj, 1, 3);\n    addEdge(adj, 1, 4);\n    addEdge(adj, 2, 3);\n    addEdge(adj, 3, 4);\n\n    printGraph(adj, V);\n    return 0;\n}<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>Divide and Conquer<\/strong><\/h3>\n\n\n\n<p>Breaks the problem into smaller subproblems, solves each subproblem recursively, and then combines the solutions, like:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Mergesort:<\/strong> Divides the array into two halves, sorts each half, and merges the sorted halves, achieving O(n log n) complexity.<\/li>\n\n\n\n<li><strong>Quicksort:<\/strong> Divides the array into subarrays based on a pivot and sorts the subarrays, with an average complexity of O(n log n).<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-3-memoization\">3. <strong>Memoization<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>An optimization technique to store the results of expensive function calls and reuse them when the same inputs occur again.<\/li>\n\n\n\n<li>Often used in conjunction with recursive algorithms to improve efficiency.<\/li>\n\n\n\n<li>Example: Recursive Fibonacci with memoization reduces the complexity from O(2^n) to O(n).<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:false,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">#include &lt;iostream&gt;\n#include &lt;vector&gt;\n\nint fibonacci(int n, std::vector&lt;int&gt;&amp; memo) {\n    if (n &lt;= 1) return n;\n    if (memo[n] != -1) return memo[n];\n    memo[n] = fibonacci(n - 1, memo) + fibonacci(n - 2, memo);\n    return memo[n];\n}\n\nint main() {\n    int n = 30;\n    std::vector&lt;int&gt; memo(n + 1, -1);\n    std::cout &lt;&lt; &quot;Fibonacci of &quot; &lt;&lt; n &lt;&lt; &quot; is &quot; &lt;&lt; fibonacci(n, memo) &lt;&lt; std::endl;\n    return 0;\n}<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-4-precomputation-and-caching\">4. <strong>Precomputation and Caching<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Precompute results for possible inputs and store them for quick lookup during execution.<\/li>\n\n\n\n<li><strong>Prefix Sum Arrays Example:<\/strong> Allows for quick range sum queries in O(1) time after an O(n) preprocessing step.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:false,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">#include &lt;iostream&gt;\n#include &lt;vector&gt;\n\nstd::vector&lt;int&gt; computePrefixSum(const std::vector&lt;int&gt;&amp; nums) {\n    std::vector&lt;int&gt; prefixSum(nums.size());\n    prefixSum[0] = nums[0];\n    for (size_t i = 1; i &lt; nums.size(); ++i) {\n        prefixSum[i] = prefixSum[i - 1] + nums[i];\n    }\n    return prefixSum;\n}\n\nint rangeSum(const std::vector&lt;int&gt;&amp; prefixSum, int left, int right) {\n    if (left == 0) return prefixSum[right];\n    return prefixSum[right] - prefixSum[left - 1];\n}\n\nint main() {\n    std::vector&lt;int&gt; nums = {1, 2, 3, 4, 5};\n    std::vector&lt;int&gt; prefixSum = computePrefixSum(nums);\n\n    int left = 1, right = 3;\n    std::cout &lt;&lt; &quot;Sum of range [&quot; &lt;&lt; left &lt;&lt; &quot;, &quot; &lt;&lt; right &lt;&lt; &quot;] is &quot; &lt;&lt; rangeSum(prefixSum, left, right) &lt;&lt; std::endl;\n    return 0;\n}<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-5-sorting-and-searching-optimization\">5. <strong>Sorting and Searching Optimization<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Optimize the choice of sorting algorithms based on input characteristics (e.g., using Timsort for real-world data).<\/li>\n\n\n\n<li>Use efficient search algorithms like binary search (O(log n)) for sorted data.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:false,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">#include &lt;iostream&gt;\n#include &lt;vector&gt;\n#include &lt;algorithm&gt;\n\nint binarySearch(const std::vector&lt;int&gt;&amp; arr, int target) {\n    int left = 0, right = arr.size() - 1;\n    while (left &lt;= right) {\n        int mid = left + (right - left) \/ 2;\n        if (arr[mid] == target) return mid;\n        if (arr[mid] &lt; target) left = mid + 1;\n        else right = mid - 1;\n    }\n    return -1;\n}\n\nint main() {\n    std::vector&lt;int&gt; arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};\n    int target = 7;\n    std::cout &lt;&lt; &quot;Index of &quot; &lt;&lt; target &lt;&lt; &quot; is &quot; &lt;&lt; binarySearch(arr, target) &lt;&lt; std::endl;\n    return 0;\n}<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-6-space-time-trade-offs\">6. <strong>Space-Time Trade-offs<\/strong><\/h3>\n\n\n\n<p>Space-time trade-offs involve balancing memory usage against execution speed. In some cases, you can reduce the time complexity of an algorithm by using more memory, or vice versa. Here&#8217;s a C++ example demonstrating this concept using the Fibonacci sequence.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-6-1-n-aive-recursive-approach-time-optimized-space-inefficient\">6.1. <strong>N<\/strong>aive<strong> Recursive Approach (Time Optimized, Space Inefficient)<\/strong><\/h4>\n\n\n\n<p>The naive recursive approach to calculate Fibonacci numbers has an exponential time complexity of O(2^n) and uses a lot of stack space due to repeated calculations of the same values.<\/p>\n\n\n\n<p><strong>Code:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:false,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">#include &lt;iostream&gt;\n\nint fibonacci(int n) {\n    if (n &lt;= 1) return n;\n    return fibonacci(n - 1) + fibonacci(n - 2);\n}\n\nint main() {\n    int n = 30; \/\/ Be careful with large values of n as it will take significant time\n    std::cout &lt;&lt; &quot;Fibonacci of &quot; &lt;&lt; n &lt;&lt; &quot; is &quot; &lt;&lt; fibonacci(n) &lt;&lt; std::endl;\n    return 0;\n}<\/pre><\/div>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-6-2-dynamic-programming-with-memoization-time-optimized-space-optimized\">6.2. Dynamic Programming with Memoization (Time Optimized, Space Optimized)<\/h4>\n\n\n\n<p>Memoization stores previously calculated results, reducing the time complexity to O(n) at the expense of additional memory.<\/p>\n\n\n\n<p><strong>Code:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:false,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">#include &lt;iostream&gt;\n#include &lt;vector&gt;\n\nint fibonacci(int n, std::vector&lt;int&gt;&amp; memo) {\n    if (n &lt;= 1) return n;\n    if (memo[n] != -1) return memo[n];\n    memo[n] = fibonacci(n - 1, memo) + fibonacci(n - 2, memo);\n    return memo[n];\n}\n\nint main() {\n    int n = 30;\n    std::vector&lt;int&gt; memo(n + 1, -1);\n    std::cout &lt;&lt; &quot;Fibonacci of &quot; &lt;&lt; n &lt;&lt; &quot; is &quot; &lt;&lt; fibonacci(n, memo) &lt;&lt; std::endl;\n    return 0;\n}<\/pre><\/div>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-6-3-iterative-approach-time-efficient-space-efficient\">6.3. Iterative Approach (Time Efficient, Space Efficient)<\/h4>\n\n\n\n<p>The iterative approach calculates Fibonacci numbers with O(n) time complexity and O(1) space complexity.<\/p>\n\n\n\n<p><strong>Code:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:false,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">#include &lt;iostream&gt;\n\nint fibonacci(int n) {\n    if (n &lt;= 1) return n;\n    int prev2 = 0, prev1 = 1, current;\n    for (int i = 2; i &lt;= n; ++i) {\n        current = prev1 + prev2;\n        prev2 = prev1;\n        prev1 = current;\n    }\n    return current;\n}\n\nint main() {\n    int n = 30;\n    std::cout &lt;&lt; &quot;Fibonacci of &quot; &lt;&lt; n &lt;&lt; &quot; is &quot; &lt;&lt; fibonacci(n) &lt;&lt; std::endl;\n    return 0;\n}<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation of Space-Time Trade-offs<\/h3>\n\n\n\n<p>1.<strong>Naive Recursive Approach:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Complexity:<\/strong> O(2^n) because it recalculates Fibonacci numbers multiple times.<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong> O(n) due to the call stack used in recursion.<\/li>\n<\/ul>\n\n\n\n<p>2.<strong>Dynamic Programming with Memoization:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Complexity:<\/strong> O(n) as it calculates each Fibonacci number only once.<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong> O(n) for storing the results of previous calculations in a memo array.<\/li>\n<\/ul>\n\n\n\n<p>3.<strong>Iterative Approach:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Complexity:<\/strong> O(n) for calculating the Fibonacci sequence.<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong> O(1) as it only uses a few variables to store intermediate results.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-7-parallelism-and-concurrency\">7. <strong>Parallelism and Concurrency<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Split the work across multiple processors or threads to reduce the running time.<\/li>\n\n\n\n<li>Examples:<\/li>\n\n\n\n<li><strong>MapReduce:<\/strong> A framework for processing large data sets with a distributed algorithm on a cluster.<\/li>\n\n\n\n<li><strong>Parallel Sorting Algorithms:<\/strong> Such as parallel mergesort.<\/li>\n<\/ul>\n\n\n\n<p>By employing these techniques, you can often significantly improve the efficiency of your algorithms, making your applications faster and more scalable.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-conclusion\">Conclusion<\/h3>\n\n\n\n<p>Big-O notation is a fundamental concept for understanding and analyzing the efficiency of algorithms in C++. By providing a high-level understanding of the algorithm&#8217;s growth rate, it allows developers to predict performance, ensure scalability, and optimize their code effectively. When designing and implementing algorithms, always consider their Big-O complexity to make informed decisions about their suitability and efficiency for the given problem.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Understanding algorithm complexity, specifically Big-O notation, is crucial for analyzing and comparing the efficiency of algorithms. In C++, as in other programming languages, the complexity of an algorithm can significantly impact the performance and scalability of an application. So it&#8217;s useful to know how to improve the algorithm complexity. But before that let&#8217;s explore the &hellip; <a href=\"https:\/\/cppdepend.com\/blog\/7-tips-to-improve-the-c-algorithms-complexity-big-o\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;7 Tips to improve the C++ algorithms complexity (Big-O)&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[720,708,707,188,713,721,211,139,710,719,709,711,717,281,712,718,715,714,716,722],"class_list":["post-1939","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-algorithm-analysis","tag-big-o-notation","tag-c-algorithms","tag-c-performance","tag-caching","tag-coding-best-practices","tag-concurrency","tag-data-structures","tag-divide-and-conquer","tag-efficient-algorithms","tag-improving-algorithm-complexity","tag-memoization","tag-parallelism","tag-performance-optimization","tag-precomputation","tag-scalable-code","tag-searching-optimization","tag-sorting","tag-space-time-trade-offs","tag-technical-tips"],"_links":{"self":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/1939","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/comments?post=1939"}],"version-history":[{"count":12,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/1939\/revisions"}],"predecessor-version":[{"id":1951,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/1939\/revisions\/1951"}],"wp:attachment":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/media?parent=1939"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/categories?post=1939"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/tags?post=1939"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}