{"id":48448,"date":"2025-05-07T16:31:22","date_gmt":"2025-05-07T11:01:22","guid":{"rendered":"https:\/\/www.iquanta.in\/blog\/?p=48448"},"modified":"2025-05-07T16:31:25","modified_gmt":"2025-05-07T11:01:25","slug":"insertion-sort-algorithm-with-complexity-analysis","status":"publish","type":"post","link":"https:\/\/www.iquanta.in\/blog\/insertion-sort-algorithm-with-complexity-analysis\/","title":{"rendered":"Insertion Sort Algorithm with Complexity Analysis"},"content":{"rendered":"\n<p>Sorting is a common task in programming, and the insertion sort algorithm is one of the easiest ways to understand how sorting works. If you are just starting with algorithms or preparing for interviews then this guide is all you need to understand the insertion sort algorithm from the ground up.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_77 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.iquanta.in\/blog\/insertion-sort-algorithm-with-complexity-analysis\/#What_is_Insertion_Sort_Algorithm\" >What is Insertion Sort Algorithm?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.iquanta.in\/blog\/insertion-sort-algorithm-with-complexity-analysis\/#How_Insertion_Sort_Work\" >How Insertion Sort Work?<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.iquanta.in\/blog\/insertion-sort-algorithm-with-complexity-analysis\/#Step-by-Step_Explanation\" >Step-by-Step Explanation<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.iquanta.in\/blog\/insertion-sort-algorithm-with-complexity-analysis\/#Complexity_Analysis_of_Insertion_Sort\" >Complexity Analysis of Insertion Sort<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.iquanta.in\/blog\/insertion-sort-algorithm-with-complexity-analysis\/#Implementation_of_Insertion_Sort_in_C\" >Implementation of Insertion Sort in C++<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.iquanta.in\/blog\/insertion-sort-algorithm-with-complexity-analysis\/#Implementation_of_Insertion_Sort_in_Python\" >Implementation of Insertion Sort in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.iquanta.in\/blog\/insertion-sort-algorithm-with-complexity-analysis\/#Advantages_of_Insertion_Sort_Algorithm\" >Advantages of Insertion Sort Algorithm<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.iquanta.in\/blog\/insertion-sort-algorithm-with-complexity-analysis\/#Disadvantages_of_Insertion_Sort_Algorithm\" >Disadvantages of Insertion Sort Algorithm<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.iquanta.in\/blog\/insertion-sort-algorithm-with-complexity-analysis\/#FAQs_Insertion_Sort_Algorithm\" >FAQs (Insertion Sort Algorithm)<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.iquanta.in\/blog\/insertion-sort-algorithm-with-complexity-analysis\/#What_is_the_insertion_sort_algorithm_used_for\" >What is the insertion sort algorithm used for?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.iquanta.in\/blog\/insertion-sort-algorithm-with-complexity-analysis\/#Is_insertion_sort_better_than_bubble_sort\" >Is insertion sort better than bubble sort?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/www.iquanta.in\/blog\/insertion-sort-algorithm-with-complexity-analysis\/#Does_insertion_sort_work_in-place\" >Does insertion sort work in-place?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"h-what-is-insertion-sort-algorithm\"><span class=\"ez-toc-section\" id=\"What_is_Insertion_Sort_Algorithm\"><\/span><strong>What is Insertion Sort Algorithm?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The insertion sort algorithm is a simple and beginner-friendly method used to sort elements in a list. It works just like how we sort playing cards in our hands. Imagine you are holding a few unsorted cards. You pick one card at a time and place it in the correct position among the cards you&#8217;ve already sorted. You keep doing this until all the cards are in order.<\/p>\n\n\n\n<p>In the same way, the insertion sort algorithm starts from the second element in a list which compares it with the elements before it and inserts it into its right position. This process continues until the whole list is sorted.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img fetchpriority=\"high\" decoding=\"async\" width=\"444\" height=\"1024\" src=\"https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/image-35-444x1024.png\" alt=\"insertion sort algorithm\" class=\"wp-image-48463\" style=\"width:462px;height:auto\" srcset=\"https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/image-35-444x1024.png 444w, https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/image-35-130x300.png 130w, https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/image-35-182x420.png 182w, https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/image-35-150x346.png 150w, https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/image-35-300x692.png 300w, https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/image-35.png 691w\" sizes=\"(max-width: 444px) 100vw, 444px\" \/><\/figure><\/div>\n\n\n<p>Let\u2019s walk through a step-by-step example using this list:<br>[1, 7, 5, 12,<strong> <\/strong>9, 6, 4, 2]<\/p>\n\n\n\n<p>We assume the first element 1 is already sorted. Now we go through each element one by one:<\/p>\n\n\n\n<ol>\n<li>Compare 7 with 1 \u2013 Since 7 is greater, it stays where it is.<br> Array = [1, 7, 5, 12, 9, 6, 4, 2]<\/li>\n\n\n\n<li>Compare 5 with 7 \u2013 5 is smaller, so we move 7 to the right and insert 5 before it.<br> Array = [1, 5, 7, 12, 9, 6, 4, 2]<\/li>\n\n\n\n<li>Compare 12 with 7 \u2013 12 is greater, no change needed.<br>Array =  [1, 5, 7, 12, 9, 6, 4, 2]<\/li>\n\n\n\n<li>Compare 9 with 12 \u2013 9 is smaller, move 12 and insert 9 before it.<br>Array = [1, 5, 7, 9, 12, 6, 4, 2]<\/li>\n\n\n\n<li>Compare 6 with 12, 9, and 7 \u2013 Move all three to the right and insert 6 at the correct place.<br>Array = [1, 5, 6, 7, 9, 12, 4, 2]<\/li>\n\n\n\n<li>Compare 4 with 12, 9, 7, 6, and 5 \u2013 Move all these and place 4 after 1.<br>Array = [1, 4, 5, 6, 7, 9, 12, 2]<\/li>\n\n\n\n<li>Compare 2 with 12 down to 1 \u2013 Shift all greater elements and place 2 after 1.<br>Resultant Array = [1, 2, 4, 5, 6, 7, 9, 12]<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/chat.whatsapp.com\/B6weknl7133BQXjPva0pgB\"><img decoding=\"async\" width=\"875\" height=\"142\" src=\"https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/image-30.png\" alt=\"INSERTION SORT ALGORITHM\" class=\"wp-image-48423\" srcset=\"https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/image-30.png 875w, https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/image-30-300x49.png 300w, https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/image-30-768x125.png 768w, https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/image-30-150x24.png 150w, https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/image-30-696x113.png 696w\" sizes=\"(max-width: 875px) 100vw, 875px\" \/><\/a><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-how-insertion-sort-work\"><span class=\"ez-toc-section\" id=\"How_Insertion_Sort_Work\"><\/span><strong>How Insertion Sort Work?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The insertion sort algorithm starts from the second element of the list and assumes the first one is already sorted. Then it compares each new element with the sorted portion and inserts it in the right place.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Step-by-Step_Explanation\"><\/span><strong>Step-by-Step Explanation<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Let\u2019s consider an example list:<br>[5, 3, 4, 1, 2]<\/p>\n\n\n\n<ul>\n<li>In Phase 1 Current element = 3<\/li>\n\n\n\n<li>Compare with 5 and shift 5 to the right<\/li>\n\n\n\n<li>Insert 3 at index 0<\/li>\n\n\n\n<li>List becomes: [3, 5, 4, 1, 2]<\/li>\n<\/ul>\n\n\n\n<p>Phase 2<\/p>\n\n\n\n<ul>\n<li>Current element = 4<\/li>\n\n\n\n<li>Compare with 5  and shift 5<\/li>\n\n\n\n<li>Compare with 3 and no shift<\/li>\n\n\n\n<li>Insert 4 after 3<\/li>\n\n\n\n<li>List becomes: [3, 4, 5, 1, 2]<\/li>\n<\/ul>\n\n\n\n<p>Phase 3<\/p>\n\n\n\n<ul>\n<li>Current element = 1<\/li>\n\n\n\n<li>Shift 5, 4, 3<\/li>\n\n\n\n<li>Insert 1 at index 0<\/li>\n\n\n\n<li>List becomes: [1, 3, 4, 5, 2]<\/li>\n<\/ul>\n\n\n\n<p>4th Step <\/p>\n\n\n\n<ul>\n<li>Current element = 2<\/li>\n\n\n\n<li>Shift 5, 4, 3<\/li>\n\n\n\n<li>Insert 2 after 1<\/li>\n\n\n\n<li>Final sorted list: [1, 2, 3, 4, 5]<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-complexity-analysis-of-insertion-sort\"><span class=\"ez-toc-section\" id=\"Complexity_Analysis_of_Insertion_Sort\"><\/span><strong>Complexity Analysis of Insertion Sort<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><thead><tr><th>Step<\/th><th>Current Element<\/th><th>Comparison Elements<\/th><th>Action Taken<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>7<\/td><td>[1]<\/td><td>7 &gt; 1     Keep 7 in place<\/td><\/tr><tr><td>2<\/td><td>5<\/td><td>[1, 7]<\/td><td>5 &lt; 7     Move 7 and Insert 5 before it<\/td><\/tr><tr><td>3<\/td><td>12<\/td><td>[1, 5, 7]<\/td><td>12 &gt; 7   Keep in place<\/td><\/tr><tr><td>4<\/td><td>9<\/td><td>[1, 5, 7, 12]<\/td><td>9 &lt; 12   Move 12, Insert before it<\/td><\/tr><tr><td>5<\/td><td>6<\/td><td>[1, 5, 7, 9, 12]<\/td><td>6 &lt; 9     Shift 9, 7 \u2192 Insert after 5<\/td><\/tr><tr><td>6<\/td><td>4<\/td><td>[1, 5, 6, 7, 9, 12]<\/td><td>4 &lt; 5     Shift multiple, insert after 1<\/td><\/tr><tr><td>7<\/td><td>2<\/td><td>[1, 4, 5, 6, 7, 9, 12]<\/td><td>2 &lt; 4     Shift multiple, insert after 1<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">Comparison of Elements in Insertion Sort Algorithm<\/figcaption><\/figure>\n\n\n\n<p>In the second table which is mentioned below we are comparing Insertion Sort with Other Algorithm like <a href=\"https:\/\/www.iquanta.in\/blog\/bubble-sort-algorithm-with-complexity-analysis\/\">Bubble Sort<\/a>, Selection Sort, Merge Sort and Quick Sort.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><thead><tr><th>Sorting Algorithm<\/th><th>Time (Best)<\/th><th>Time (Avg)<\/th><th>Time (Worst)<\/th><th>Space<\/th><th>Stable<\/th><th>Use Case<\/th><\/tr><\/thead><tbody><tr><td><strong>Insertion Sort<\/strong><\/td><td>O(n)<\/td><td>O(n\u00b2)<\/td><td>O(n\u00b2)<\/td><td>O(1)<\/td><td>Yes<\/td><td>Small or nearly sorted arrays<\/td><\/tr><tr><td>Bubble Sort<\/td><td>O(n)<\/td><td>O(n\u00b2)<\/td><td>O(n\u00b2)<\/td><td>O(1)<\/td><td>Yes<\/td><td>Educational use and very simple<\/td><\/tr><tr><td>Selection Sort<\/td><td>O(n\u00b2)<\/td><td>O(n\u00b2)<\/td><td>O(n\u00b2)<\/td><td>O(1)<\/td><td>No<\/td><td>When swaps are more expensive than comparisons<\/td><\/tr><tr><td>Merge Sort<\/td><td>O(n log n)<\/td><td>O(n log n)<\/td><td>O(n log n)<\/td><td>O(n)<\/td><td>Yes<\/td><td>Large datasets<\/td><\/tr><tr><td>Quick Sort<\/td><td>O(n log n)<\/td><td>O(n log n)<\/td><td>O(n\u00b2)<\/td><td>O(log n)<\/td><td>No<\/td><td>Fastest in practice<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">Comparison of Insertion Sort with Oher Sorting Algorithms<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-implementation-of-insertion-sort-in-c-0\"><span class=\"ez-toc-section\" id=\"Implementation_of_Insertion_Sort_in_C\"><\/span><strong>Implementation of Insertion Sort in C++<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\r\nusing namespace std;\r\n\r\n\/\/ Function to perform insertion sort\r\nvoid insertionSort(int arr&#091;], int n) {\r\n    for (int i = 1; i &lt; n; i++) {\r\n        int key = arr&#091;i];\r\n        int j = i - 1;\r\n\r\n        \/\/ Shift elements greater than key to the right\r\n        while (j &gt;= 0 &amp;&amp; arr&#091;j] &gt; key) {\r\n            arr&#091;j + 1] = arr&#091;j];\r\n            j--;\r\n        }\r\n\r\n        arr&#091;j + 1] = key;\r\n    }\r\n}\r\n\r\n\/\/ Function to print the array\r\nvoid printArray(int arr&#091;], int n) {\r\n    for (int i = 0; i &lt; n; i++)\r\n        cout &lt;&lt; arr&#091;i] &lt;&lt; \" \";\r\n    cout &lt;&lt; endl;\r\n}\r\n\r\n\/\/ Driver code\r\nint main() {\r\n    int arr&#091;] = {1, 7, 5, 12, 9, 6, 4, 2};\r\n    int n = sizeof(arr) \/ sizeof(arr&#091;0]);\r\n\r\n    cout &lt;&lt; \"Original array:\\n\";\r\n    printArray(arr, n);\r\n\r\n    insertionSort(arr, n);\r\n\r\n    cout &lt;&lt; \"Sorted array:\\n\";\r\n    printArray(arr, n);\r\n\r\n    return 0;\r\n}\r<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-implementation-of-insertion-sort-in-python\"><span class=\"ez-toc-section\" id=\"Implementation_of_Insertion_Sort_in_Python\"><\/span><strong>Implementation of Insertion Sort in Python<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>def insertion_sort(arr):\r\n    # Traverse from 1 to len(arr)\r\n    for i in range(1, len(arr)):\r\n        key = arr&#091;i]\r\n        j = i - 1\r\n\r\n        # Move elements of arr&#091;0..i-1], that are greater than key, one position ahead\r\n        while j &gt;= 0 and arr&#091;j] &gt; key:\r\n            arr&#091;j + 1] = arr&#091;j]\r\n            j -= 1\r\n\r\n        arr&#091;j + 1] = key\r\n\r\n# Driver code\r\narr = &#091;1, 7, 5, 12, 9, 6, 4, 2]\r\nprint(\"Original array:\")\r\nprint(arr)\r\n\r\ninsertion_sort(arr)\r\n\r\nprint(\"Sorted array:\")\r\nprint(arr)\r<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-advantages-of-insertion-sort-algorithm\"><span class=\"ez-toc-section\" id=\"Advantages_of_Insertion_Sort_Algorithm\"><\/span><strong>Advantages of Insertion Sort Algorithm<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ol>\n<li>Simple to implement and understand.<\/li>\n\n\n\n<li>Efficient for small or nearly sorted datasets.<\/li>\n\n\n\n<li>In-place sorting with stable behavior.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-disadvantages-of-insertion-sort-algorithm\"><span class=\"ez-toc-section\" id=\"Disadvantages_of_Insertion_Sort_Algorithm\"><\/span><strong>Disadvantages of Insertion Sort Algorithm<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ol>\n<li>Insertion Sort is slow for large datasets because its time complexity is O(n\u00b2).<\/li>\n\n\n\n<li>It is not ideal for large or completely unsorted data as it performs poorly in such cases.<\/li>\n\n\n\n<li>It requires a lot of comparisons and shifting of elements in the worst case which makes it inefficient.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-faqs-insertion-sort-algorithm\"><span class=\"ez-toc-section\" id=\"FAQs_Insertion_Sort_Algorithm\"><\/span><strong>FAQs (Insertion Sort Algorithm)<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_is_the_insertion_sort_algorithm_used_for\"><\/span><strong>What is the insertion sort algorithm used for?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>The insertion sort algorithm is mainly used to sort small datasets or nearly sorted arrays. It&#8217;s a great choice when performance is not the top priority but simplicity and clarity are.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Is_insertion_sort_better_than_bubble_sort\"><\/span><strong>Is insertion sort better than bubble sort?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Yes, insertion sort usually performs better than bubble sort. It does fewer comparisons and swaps in practice, especially when the data is already partially sorted.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Does_insertion_sort_work_in-place\"><\/span><strong>Does insertion sort work in-place?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Yes, insertion sort is an in-place sorting algorithm. It doesn\u2019t require any extra memory space and modifies the original array directly during the sorting process.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sorting is a common task in programming, and the insertion sort algorithm is one of the easiest ways to understand how sorting works. If you are just starting with algorithms or preparing for interviews then this guide is all you need to understand the insertion sort algorithm from the ground up. What is Insertion Sort [&hellip;]<\/p>\n","protected":false},"author":560,"featured_media":48467,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1075,1073],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.4 (Yoast SEO v21.9.1) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Insertion Sort Algorithm with Complexity Analysis - iQuanta<\/title>\n<meta name=\"description\" content=\"If you are just starting with algorithms or preparing for interviews then you need to understand the insertion sort algorithm.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.iquanta.in\/blog\/insertion-sort-algorithm-with-complexity-analysis\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Insertion Sort Algorithm with Complexity Analysis\" \/>\n<meta property=\"og:description\" content=\"If you are just starting with algorithms or preparing for interviews then you need to understand the insertion sort algorithm.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.iquanta.in\/blog\/insertion-sort-algorithm-with-complexity-analysis\/\" \/>\n<meta property=\"og:site_name\" content=\"iQuanta\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/facebook.com\/iquanta.in\" \/>\n<meta property=\"article:published_time\" content=\"2025-05-07T11:01:22+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-05-07T11:01:25+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/Your-paragraph-text-14.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1600\" \/>\n\t<meta property=\"og:image:height\" content=\"900\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Nidhi Goswami\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Nidhi Goswami\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.iquanta.in\/blog\/insertion-sort-algorithm-with-complexity-analysis\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.iquanta.in\/blog\/insertion-sort-algorithm-with-complexity-analysis\/\"},\"author\":{\"name\":\"Nidhi Goswami\",\"@id\":\"https:\/\/www.iquanta.in\/blog\/#\/schema\/person\/ec8c8c25d0526dd86557b6fed064f7f3\"},\"headline\":\"Insertion Sort Algorithm with Complexity Analysis\",\"datePublished\":\"2025-05-07T11:01:22+00:00\",\"dateModified\":\"2025-05-07T11:01:25+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.iquanta.in\/blog\/insertion-sort-algorithm-with-complexity-analysis\/\"},\"wordCount\":757,\"publisher\":{\"@id\":\"https:\/\/www.iquanta.in\/blog\/#organization\"},\"articleSection\":[\"DSA and Competitive Programming\",\"iSkills\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.iquanta.in\/blog\/insertion-sort-algorithm-with-complexity-analysis\/\",\"url\":\"https:\/\/www.iquanta.in\/blog\/insertion-sort-algorithm-with-complexity-analysis\/\",\"name\":\"Insertion Sort Algorithm with Complexity Analysis - iQuanta\",\"isPartOf\":{\"@id\":\"https:\/\/www.iquanta.in\/blog\/#website\"},\"datePublished\":\"2025-05-07T11:01:22+00:00\",\"dateModified\":\"2025-05-07T11:01:25+00:00\",\"description\":\"If you are just starting with algorithms or preparing for interviews then you need to understand the insertion sort algorithm.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.iquanta.in\/blog\/insertion-sort-algorithm-with-complexity-analysis\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.iquanta.in\/blog\/insertion-sort-algorithm-with-complexity-analysis\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.iquanta.in\/blog\/insertion-sort-algorithm-with-complexity-analysis\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.iquanta.in\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Insertion Sort Algorithm with Complexity Analysis\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.iquanta.in\/blog\/#website\",\"url\":\"https:\/\/www.iquanta.in\/blog\/\",\"name\":\"iQuanta | Cat Preparation Online\",\"description\":\"Building Learning Networks\",\"publisher\":{\"@id\":\"https:\/\/www.iquanta.in\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.iquanta.in\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.iquanta.in\/blog\/#organization\",\"name\":\"IQuanta\",\"url\":\"https:\/\/www.iquanta.in\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.iquanta.in\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2018\/08\/IQuanta-1.png\",\"contentUrl\":\"https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2018\/08\/IQuanta-1.png\",\"width\":525,\"height\":200,\"caption\":\"IQuanta\"},\"image\":{\"@id\":\"https:\/\/www.iquanta.in\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/facebook.com\/iquanta.in\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.iquanta.in\/blog\/#\/schema\/person\/ec8c8c25d0526dd86557b6fed064f7f3\",\"name\":\"Nidhi Goswami\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.iquanta.in\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/21d234d87afd924b217d26b25a3cf1ee?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/21d234d87afd924b217d26b25a3cf1ee?s=96&d=mm&r=g\",\"caption\":\"Nidhi Goswami\"},\"url\":\"https:\/\/www.iquanta.in\/blog\/author\/nidhigoswami\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Insertion Sort Algorithm with Complexity Analysis - iQuanta","description":"If you are just starting with algorithms or preparing for interviews then you need to understand the insertion sort algorithm.","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:\/\/www.iquanta.in\/blog\/insertion-sort-algorithm-with-complexity-analysis\/","og_locale":"en_US","og_type":"article","og_title":"Insertion Sort Algorithm with Complexity Analysis","og_description":"If you are just starting with algorithms or preparing for interviews then you need to understand the insertion sort algorithm.","og_url":"https:\/\/www.iquanta.in\/blog\/insertion-sort-algorithm-with-complexity-analysis\/","og_site_name":"iQuanta","article_publisher":"https:\/\/facebook.com\/iquanta.in","article_published_time":"2025-05-07T11:01:22+00:00","article_modified_time":"2025-05-07T11:01:25+00:00","og_image":[{"width":1600,"height":900,"url":"https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/Your-paragraph-text-14.png","type":"image\/png"}],"author":"Nidhi Goswami","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Nidhi Goswami","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.iquanta.in\/blog\/insertion-sort-algorithm-with-complexity-analysis\/#article","isPartOf":{"@id":"https:\/\/www.iquanta.in\/blog\/insertion-sort-algorithm-with-complexity-analysis\/"},"author":{"name":"Nidhi Goswami","@id":"https:\/\/www.iquanta.in\/blog\/#\/schema\/person\/ec8c8c25d0526dd86557b6fed064f7f3"},"headline":"Insertion Sort Algorithm with Complexity Analysis","datePublished":"2025-05-07T11:01:22+00:00","dateModified":"2025-05-07T11:01:25+00:00","mainEntityOfPage":{"@id":"https:\/\/www.iquanta.in\/blog\/insertion-sort-algorithm-with-complexity-analysis\/"},"wordCount":757,"publisher":{"@id":"https:\/\/www.iquanta.in\/blog\/#organization"},"articleSection":["DSA and Competitive Programming","iSkills"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.iquanta.in\/blog\/insertion-sort-algorithm-with-complexity-analysis\/","url":"https:\/\/www.iquanta.in\/blog\/insertion-sort-algorithm-with-complexity-analysis\/","name":"Insertion Sort Algorithm with Complexity Analysis - iQuanta","isPartOf":{"@id":"https:\/\/www.iquanta.in\/blog\/#website"},"datePublished":"2025-05-07T11:01:22+00:00","dateModified":"2025-05-07T11:01:25+00:00","description":"If you are just starting with algorithms or preparing for interviews then you need to understand the insertion sort algorithm.","breadcrumb":{"@id":"https:\/\/www.iquanta.in\/blog\/insertion-sort-algorithm-with-complexity-analysis\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.iquanta.in\/blog\/insertion-sort-algorithm-with-complexity-analysis\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.iquanta.in\/blog\/insertion-sort-algorithm-with-complexity-analysis\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.iquanta.in\/blog\/"},{"@type":"ListItem","position":2,"name":"Insertion Sort Algorithm with Complexity Analysis"}]},{"@type":"WebSite","@id":"https:\/\/www.iquanta.in\/blog\/#website","url":"https:\/\/www.iquanta.in\/blog\/","name":"iQuanta | Cat Preparation Online","description":"Building Learning Networks","publisher":{"@id":"https:\/\/www.iquanta.in\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.iquanta.in\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.iquanta.in\/blog\/#organization","name":"IQuanta","url":"https:\/\/www.iquanta.in\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.iquanta.in\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2018\/08\/IQuanta-1.png","contentUrl":"https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2018\/08\/IQuanta-1.png","width":525,"height":200,"caption":"IQuanta"},"image":{"@id":"https:\/\/www.iquanta.in\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/facebook.com\/iquanta.in"]},{"@type":"Person","@id":"https:\/\/www.iquanta.in\/blog\/#\/schema\/person\/ec8c8c25d0526dd86557b6fed064f7f3","name":"Nidhi Goswami","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.iquanta.in\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/21d234d87afd924b217d26b25a3cf1ee?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/21d234d87afd924b217d26b25a3cf1ee?s=96&d=mm&r=g","caption":"Nidhi Goswami"},"url":"https:\/\/www.iquanta.in\/blog\/author\/nidhigoswami\/"}]}},"_links":{"self":[{"href":"https:\/\/www.iquanta.in\/blog\/wp-json\/wp\/v2\/posts\/48448"}],"collection":[{"href":"https:\/\/www.iquanta.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.iquanta.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.iquanta.in\/blog\/wp-json\/wp\/v2\/users\/560"}],"replies":[{"embeddable":true,"href":"https:\/\/www.iquanta.in\/blog\/wp-json\/wp\/v2\/comments?post=48448"}],"version-history":[{"count":8,"href":"https:\/\/www.iquanta.in\/blog\/wp-json\/wp\/v2\/posts\/48448\/revisions"}],"predecessor-version":[{"id":48468,"href":"https:\/\/www.iquanta.in\/blog\/wp-json\/wp\/v2\/posts\/48448\/revisions\/48468"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.iquanta.in\/blog\/wp-json\/wp\/v2\/media\/48467"}],"wp:attachment":[{"href":"https:\/\/www.iquanta.in\/blog\/wp-json\/wp\/v2\/media?parent=48448"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.iquanta.in\/blog\/wp-json\/wp\/v2\/categories?post=48448"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.iquanta.in\/blog\/wp-json\/wp\/v2\/tags?post=48448"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}