{"id":28044,"date":"2021-04-01T08:44:00","date_gmt":"2021-04-01T03:14:00","guid":{"rendered":"https:\/\/www.mygreatlearning.com\/blog\/insertion-sort-with-a-real-world-example\/"},"modified":"2025-02-25T19:27:16","modified_gmt":"2025-02-25T13:57:16","slug":"insertion-sort-with-a-real-world-example","status":"publish","type":"post","link":"https:\/\/www.mygreatlearning.com\/blog\/insertion-sort-with-a-real-world-example\/","title":{"rendered":"Insertion Sort with a Real-World Example"},"content":{"rendered":"\n<p>Insertion Sort is one of the fundamental sorting algorithms that is simple yet powerful. It works similarly to how we arrange playing cards in our hands by inserting each new card into its correct position.<\/p>\n\n\n\n<p>Although it is not the most efficient sorting algorithm, understanding it provides a solid foundation for learning more advanced sorting techniques.<\/p>\n\n\n\n<p>In this guide, we will <strong>explore the Insertion Sort algorithm in-depth<\/strong>, understand its <strong>step-by-step working<\/strong>, solve <strong>practical problems<\/strong>, and discuss <strong>real-world applications<\/strong> to help you master this sorting technique.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-does-insertion-sort-work\">How Does Insertion Sort Work?<\/h2>\n\n\n\n<p>Insertion Sort builds a sorted array one element at a time by comparing the current element with the ones before it and inserting it into its correct position.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"algorithm\"><strong>Algorithm<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Start from the <strong>second element<\/strong> (index 1) because we assume the first element is already sorted.<\/li>\n\n\n\n<li>Store the <strong>current element<\/strong> (key) and compare it with the elements before it.<\/li>\n\n\n\n<li><strong>Shift<\/strong> all larger elements <strong>one position to the right<\/strong> to make space for the key.<\/li>\n\n\n\n<li>Insert the <strong>key<\/strong> into its correct position.<\/li>\n\n\n\n<li>Repeat the process for all elements in the array.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"step-by-step-example\"><strong>Step-by-Step Example<\/strong><\/h3>\n\n\n\n<p>Let's sort the array:<br><strong>arr = [5, 3, 8, 6, 2]<\/strong><\/p>\n\n\n\n<p><strong>Initial Array:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;5, 3, 8, 6, 2]\n<\/pre><\/div>\n\n\n<p>Pass 1 (i = 1, key = 3)<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Compare 3 with 5 \u2192 <strong>Move 5 to the right<\/strong><\/li>\n\n\n\n<li>Insert 3 at position <strong>0<\/strong><\/li>\n<\/ul>\n\n\n\n<p><strong>Array after Pass 1:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;3, 5, 8, 6, 2]\n<\/pre><\/div>\n\n\n<p>Pass 2 (i = 2, key = 8)<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Compare 8 with 5 \u2192 <strong>No shifting needed<\/strong><\/li>\n\n\n\n<li>8 is already in the correct place<\/li>\n<\/ul>\n\n\n\n<p><strong>Array after Pass 2:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;3, 5, 8, 6, 2]\n<\/pre><\/div>\n\n\n<p>Pass 3 (i = 3, key = 6)<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Compare 6 with 8 \u2192 <strong>Move 8 to the right<\/strong><\/li>\n\n\n\n<li>Compare 6 with 5 \u2192 <strong>No shifting needed<\/strong><\/li>\n\n\n\n<li>Insert 6 at position <strong>2<\/strong><\/li>\n<\/ul>\n\n\n\n<p><strong>Array after Pass 3:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;3, 5, 6, 8, 2]\n<\/pre><\/div>\n\n\n<p>Pass 4 (i = 4, key = 2)<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Compare 2 with 8 \u2192 <strong>Move 8 to the right<\/strong><\/li>\n\n\n\n<li>Compare 2 with 6 \u2192 <strong>Move 6 to the right<\/strong><\/li>\n\n\n\n<li>Compare 2 with 5 \u2192 <strong>Move 5 to the right<\/strong><\/li>\n\n\n\n<li>Compare 2 with 3 \u2192 <strong>Move 3 to the right<\/strong><\/li>\n\n\n\n<li>Insert 2 at position <strong>0<\/strong><\/li>\n<\/ul>\n\n\n\n<p><strong>Final Sorted Array:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;2, 3, 5, 6, 8]\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"python-implementation\"><strong>Python Implementation<\/strong><\/h2>\n\n\n\n<p>Here\u2019s how you can implement Insertion Sort in Python:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ndef insertion_sort(arr):\n    for i in range(1, len(arr)):  # Start from the second element\n        key = arr&#x5B;i]  # Store the current element\n        j = i - 1\n\n        # Move elements that are greater than key to one position ahead\n        while j &gt;= 0 and arr&#x5B;j] &gt; key:\n            arr&#x5B;j + 1] = arr&#x5B;j]\n            j -= 1\n\n        arr&#x5B;j + 1] = key  # Insert the key at the correct position\n\narr = &#x5B;12, 11, 13, 5, 6]\ninsertion_sort(arr)\nprint(&quot;Sorted array:&quot;, arr)  # Output: &#x5B;5, 6, 11, 12, 13]\n<\/pre><\/div>\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"time-and-space-complexity-analysis\"><strong>Time and Space Complexity Analysis<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>Case<\/strong><\/td><td><strong>Time Complexity<\/strong><\/td><\/tr><tr><td>Best Case (Already Sorted)<\/td><td>O(n)<\/td><\/tr><tr><td>Worst Case (Reversed Order)<\/td><td>O(n\u00b2)<\/td><\/tr><tr><td>Average Case<\/td><td>O(n\u00b2)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Space Complexity:<\/strong> O(1) (since sorting is done in place)<\/li>\n<\/ul>\n\n\n\n<p class=\"block-course-highlighter\">Understand<a href=\"https:\/\/www.mygreatlearning.com\/blog\/why-is-time-complexity-essential\/\"> what time complexity is, why it is essential<\/a>, and how it impacts algorithm efficiency and performance in programming.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"advantages-and-disadvantages\"><strong>Advantages and Disadvantages<\/strong><\/h2>\n\n\n\n<p><strong>Advantages of Insertion Sort Algorithm:<\/strong><\/p>\n\n\n\n<p>\u2714\ufe0f Simple and easy to implement.<br>\u2714\ufe0f Efficient for small datasets.<br>\u2714\ufe0f In-place sorting (no extra memory required).<br>\u2714\ufe0f Stable sorting algorithm (maintains the order of equal elements).<\/p>\n\n\n\n<p><strong>Disadvantages of Insertion Sort Algorithm:<\/strong><\/p>\n\n\n\n<p>\u274c Inefficient for large datasets due to O(n\u00b2) complexity.<br>\u274c Not suitable for real-world applications involving large data.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"applications-of-insertion-sort\">Applications of Insertion Sort<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Sorting Small Data Sets<\/strong> \u2013 Efficient for small arrays.<\/li>\n\n\n\n<li><strong>Nearly Sorted Arrays<\/strong> \u2013 Runs in <strong>O(n) time<\/strong> when the array is nearly sorted.<\/li>\n\n\n\n<li><strong>Online Sorting<\/strong> \u2013 Useful when data arrives dynamically and needs sorting in real time.<\/li>\n\n\n\n<li><strong>Hybrid Sorting Algorithms<\/strong> \u2013 Used in <strong>Timsort and QuickSort<\/strong> for small partitions.<\/li>\n\n\n\n<li><strong>Handling Linked Lists<\/strong> \u2013 Works efficiently with linked lists.<\/li>\n\n\n\n<li><strong>Educational Purposes<\/strong> \u2013 Used to teach sorting concepts.<\/li>\n\n\n\n<li><strong>Used in Shell Sort<\/strong> \u2013 A variation of Insertion Sort that sorts elements at specific intervals.<\/li>\n\n\n\n<li><strong>Used in Computer Graphics<\/strong> \u2013 Used for <strong>real-time rendering<\/strong> and ordering tasks.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"problem-statements-and-their-solutions-using-insertion-sort\"><strong>Problem statements and their solutions using insertion sort<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"problem-1-sort-an-array-using-insertion-sort\"><strong>Problem 1: Sort an Array Using Insertion Sort<\/strong><\/h3>\n\n\n\n<p><strong>Problem Statement:<br><\/strong>Given an array of integers, sort the array in <strong>ascending order<\/strong> using the <strong>Insertion Sort algorithm<\/strong>.<\/p>\n\n\n\n<p><strong>Python Solution:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ndef insertion_sort(arr):\n    for i in range(1, len(arr)):  # Start from the second element\n        key = arr&#x5B;i]  # Store the current element\n        j = i - 1\n\n        # Move elements that are greater than key to one position ahead\n        while j &gt;= 0 and arr&#x5B;j] &gt; key:\n            arr&#x5B;j + 1] = arr&#x5B;j]\n            j -= 1\n\n        arr&#x5B;j + 1] = key  # Insert the key at the correct position\n\narr = &#x5B;12, 11, 13, 5, 6]\ninsertion_sort(arr)\nprint(&quot;Sorted array:&quot;, arr)  # Output: &#x5B;5, 6, 11, 12, 13]\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"problem-2-sort-an-array-in-descending-order-using-insertion-sort\"><strong>Problem 2: Sort an Array in Descending Order Using Insertion Sort<\/strong><\/h3>\n\n\n\n<p><strong>Problem Statement:<br><\/strong>Modify the <strong>Insertion Sort algorithm<\/strong> to sort an array in <strong>descending order<\/strong>.<\/p>\n\n\n\n<p><strong>Python Solution:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ndef insertion_sort_desc(arr):\n    for i in range(1, len(arr)):  # Loop through unsorted elements\n        key = arr&#x5B;i]  # Take the current element\n        j = i - 1\n\n        # Shift elements that are smaller than key\n        while j &gt;= 0 and arr&#x5B;j] &amp;lt; key:\n            arr&#x5B;j + 1] = arr&#x5B;j]\n            j -= 1\n\n        arr&#x5B;j + 1] = key  # Place key at the correct position\n\narr = &#x5B;4, 7, 2, 9, 1]\ninsertion_sort_desc(arr)\nprint(&quot;Sorted array in descending order:&quot;, arr)  # Output: &#x5B;9, 7, 4, 2, 1]\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"problem-3-count-the-number-of-shifts-in-insertion-sort\"><strong>Problem 3: Count the Number of Shifts in Insertion Sort<\/strong><\/h3>\n\n\n\n<p><strong>Problem Statement:<br><\/strong>Write a function that counts the number of <strong>shifts (element moves)<\/strong> required to sort an array using <strong>Insertion Sort<\/strong>.<\/p>\n\n\n\n<p><strong>Python Solution:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ndef count_shifts_insertion_sort(arr):\n    shifts = 0  # Initialize shift counter\n    for i in range(1, len(arr)):  # Traverse the array\n        key = arr&#x5B;i]  # Take the current element\n        j = i - 1\n        # Shift elements to make space for key\n        while j &gt;= 0 and arr&#x5B;j] &gt; key:\n            arr&#x5B;j + 1] = arr&#x5B;j]\n            j -= 1\n            shifts += 1  # Count each shift\n        arr&#x5B;j + 1] = key  # Place key at the correct position\n    return shifts  # Return total shifts count\n\narr = &#x5B;8, 4, 3, 7, 2]\nprint(&quot;Number of shifts:&quot;, count_shifts_insertion_sort(arr))  # Output: 6\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>Insertion Sort is a <strong>simple yet effective sorting algorithm<\/strong> that works by gradually building a sorted list one element at a time. It is particularly useful for <strong>small datasets<\/strong> or <strong>nearly sorted arrays<\/strong> due to its <strong>O(n) best-case time complexity<\/strong>. However, for larger datasets, <strong>Merge Sort or Quick Sort<\/strong> are preferred due to their better average-case performance.<\/p>\n\n\n\n<p><strong>Key Takeaways:<\/strong><\/p>\n\n\n\n<p>\u2705 <strong>Stable Sorting Algorithm<\/strong> (Preserves the order of duplicate elements)<br>\u2705 <strong>Efficient for Small\/Nearly Sorted Data<br><\/strong>\u2705 <strong>Not Suitable for Large Datasets<\/strong> (O(n\u00b2) worst-case complexity)<br>\u2705 <strong>Used in Online Sorting (Real-time data processing)<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"frequently-asked-questions\"><strong>Frequently Asked Questions<\/strong><\/h2>\n\n\n\n<p><strong>1. Why is Insertion Sort efficient for nearly sorted arrays?<\/strong><\/p>\n\n\n\n<p>Insertion Sort performs best when the array is almost sorted because fewer shifts are required, leading to an <strong>O(n) time complexity<\/strong>, which is much better than its usual <strong>O(n\u00b2)<\/strong> in worst cases.<\/p>\n\n\n\n<p><strong>2. Can Insertion Sort be implemented recursively?<\/strong><\/p>\n\n\n\n<p>Yes! Instead of using a loop, we can use recursion to sort the first (n-1) elements and then insert the last element into its correct position.<\/p>\n\n\n\n<p><strong>3. How does Insertion Sort compare to Bubble Sort and Selection Sort?<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Insertion Sort<\/strong> is <strong>faster<\/strong> than Bubble Sort because it reduces unnecessary swaps.<\/li>\n\n\n\n<li><strong>Selection Sort<\/strong> is <strong>better<\/strong> in terms of swaps but still takes O(n\u00b2) time.<\/li>\n\n\n\n<li>Among the three, <strong>Insertion Sort<\/strong> is preferred for small or nearly sorted arrays.<\/li>\n<\/ul>\n\n\n\n<p><strong>4. Is Insertion Sort a stable sorting algorithm?<\/strong><\/p>\n\n\n\n<p>Yes, Insertion Sort is <strong>stable<\/strong> because it <strong>does not change the relative order of equal elements<\/strong> in the array.<\/p>\n\n\n\n<p><strong>5. Can Insertion Sort be used for large datasets?<\/strong><\/p>\n\n\n\n<p>Not really. Due to its <strong>O(n\u00b2) time complexity<\/strong>, it becomes inefficient for large datasets. <strong>Merge Sort or Quick Sort<\/strong> are better options for handling large datasets efficiently.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Insertion Sort is a straightforward algorithm that sorts a list by inserting elements into their correct positions. This guide covers its step-by-step process, code implementation, time complexity analysis, and use cases for small or nearly sorted datasets.<\/p>\n","protected":false},"author":41,"featured_media":28055,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[25860],"tags":[36858],"content_type":[36252],"class_list":["post-28044","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software","tag-sorting-algorithm","content_type-tutorials"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.3 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Insertion Sort Algorithm with Examples<\/title>\n<meta name=\"description\" content=\"Master the basics of Insertion Sort, a simple yet efficient algorithm for small datasets. Explore its workings, benefits, and practical code examples.\" \/>\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.mygreatlearning.com\/blog\/insertion-sort-with-a-real-world-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Insertion Sort with a Real-World Example\" \/>\n<meta property=\"og:description\" content=\"Master the basics of Insertion Sort, a simple yet efficient algorithm for small datasets. Explore its workings, benefits, and practical code examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mygreatlearning.com\/blog\/insertion-sort-with-a-real-world-example\/\" \/>\n<meta property=\"og:site_name\" content=\"Great Learning Blog: Free Resources what Matters to shape your Career!\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/GreatLearningOfficial\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-04-01T03:14:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-02-25T13:57:16+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/iStock-1060938586.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1302\" \/>\n\t<meta property=\"og:image:height\" content=\"805\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Great Learning Editorial Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/Great_Learning\" \/>\n<meta name=\"twitter:site\" content=\"@Great_Learning\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Great Learning Editorial Team\" \/>\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.mygreatlearning.com\\\/blog\\\/insertion-sort-with-a-real-world-example\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/insertion-sort-with-a-real-world-example\\\/\"},\"author\":{\"name\":\"Great Learning Editorial Team\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\"},\"headline\":\"Insertion Sort with a Real-World Example\",\"datePublished\":\"2021-04-01T03:14:00+00:00\",\"dateModified\":\"2025-02-25T13:57:16+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/insertion-sort-with-a-real-world-example\\\/\"},\"wordCount\":863,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/insertion-sort-with-a-real-world-example\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/03\\\/iStock-1060938586.jpg\",\"keywords\":[\"sorting algorithm\"],\"articleSection\":[\"IT\\\/Software Development\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/insertion-sort-with-a-real-world-example\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/insertion-sort-with-a-real-world-example\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/insertion-sort-with-a-real-world-example\\\/\",\"name\":\"Insertion Sort Algorithm with Examples\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/insertion-sort-with-a-real-world-example\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/insertion-sort-with-a-real-world-example\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/03\\\/iStock-1060938586.jpg\",\"datePublished\":\"2021-04-01T03:14:00+00:00\",\"dateModified\":\"2025-02-25T13:57:16+00:00\",\"description\":\"Master the basics of Insertion Sort, a simple yet efficient algorithm for small datasets. Explore its workings, benefits, and practical code examples.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/insertion-sort-with-a-real-world-example\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/insertion-sort-with-a-real-world-example\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/insertion-sort-with-a-real-world-example\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/03\\\/iStock-1060938586.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/03\\\/iStock-1060938586.jpg\",\"width\":1302,\"height\":805,\"caption\":\"Insertion sort\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/insertion-sort-with-a-real-world-example\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"IT\\\/Software Development\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/software\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Insertion Sort with a Real-World Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"name\":\"Great Learning Blog\",\"description\":\"Learn, Upskill &amp; Career Development Guide and Resources\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"alternateName\":\"Great Learning\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\",\"name\":\"Great Learning\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"width\":900,\"height\":900,\"caption\":\"Great Learning\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/GreatLearningOfficial\\\/\",\"https:\\\/\\\/x.com\\\/Great_Learning\",\"https:\\\/\\\/www.instagram.com\\\/greatlearningofficial\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/in.pinterest.com\\\/greatlearning12\\\/\",\"https:\\\/\\\/www.youtube.com\\\/user\\\/beaconelearning\\\/\"],\"description\":\"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.\",\"email\":\"info@mygreatlearning.com\",\"legalName\":\"Great Learning Education Services Pvt. Ltd\",\"foundingDate\":\"2013-11-29\",\"numberOfEmployees\":{\"@type\":\"QuantitativeValue\",\"minValue\":\"1001\",\"maxValue\":\"5000\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\",\"name\":\"Great Learning Editorial Team\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"caption\":\"Great Learning Editorial Team\"},\"description\":\"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.\",\"sameAs\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/\",\"https:\\\/\\\/in.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/Great_Learning\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCObs0kLIrDjX2LLSybqNaEA\"],\"award\":[\"Best EdTech Company of the Year 2024\",\"Education Economictimes Outstanding Education\\\/Edtech Solution Provider of the Year 2024\",\"Leading E-learning Platform 2024\"],\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/author\\\/greatlearning\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Insertion Sort Algorithm with Examples","description":"Master the basics of Insertion Sort, a simple yet efficient algorithm for small datasets. Explore its workings, benefits, and practical code examples.","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.mygreatlearning.com\/blog\/insertion-sort-with-a-real-world-example\/","og_locale":"en_US","og_type":"article","og_title":"Insertion Sort with a Real-World Example","og_description":"Master the basics of Insertion Sort, a simple yet efficient algorithm for small datasets. Explore its workings, benefits, and practical code examples.","og_url":"https:\/\/www.mygreatlearning.com\/blog\/insertion-sort-with-a-real-world-example\/","og_site_name":"Great Learning Blog: Free Resources what Matters to shape your Career!","article_publisher":"https:\/\/www.facebook.com\/GreatLearningOfficial\/","article_published_time":"2021-04-01T03:14:00+00:00","article_modified_time":"2025-02-25T13:57:16+00:00","og_image":[{"width":1302,"height":805,"url":"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/iStock-1060938586.jpg","type":"image\/jpeg"}],"author":"Great Learning Editorial Team","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/Great_Learning","twitter_site":"@Great_Learning","twitter_misc":{"Written by":"Great Learning Editorial Team","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.mygreatlearning.com\/blog\/insertion-sort-with-a-real-world-example\/#article","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/insertion-sort-with-a-real-world-example\/"},"author":{"name":"Great Learning Editorial Team","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad"},"headline":"Insertion Sort with a Real-World Example","datePublished":"2021-04-01T03:14:00+00:00","dateModified":"2025-02-25T13:57:16+00:00","mainEntityOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/insertion-sort-with-a-real-world-example\/"},"wordCount":863,"commentCount":0,"publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/insertion-sort-with-a-real-world-example\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/iStock-1060938586.jpg","keywords":["sorting algorithm"],"articleSection":["IT\/Software Development"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.mygreatlearning.com\/blog\/insertion-sort-with-a-real-world-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.mygreatlearning.com\/blog\/insertion-sort-with-a-real-world-example\/","url":"https:\/\/www.mygreatlearning.com\/blog\/insertion-sort-with-a-real-world-example\/","name":"Insertion Sort Algorithm with Examples","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/insertion-sort-with-a-real-world-example\/#primaryimage"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/insertion-sort-with-a-real-world-example\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/iStock-1060938586.jpg","datePublished":"2021-04-01T03:14:00+00:00","dateModified":"2025-02-25T13:57:16+00:00","description":"Master the basics of Insertion Sort, a simple yet efficient algorithm for small datasets. Explore its workings, benefits, and practical code examples.","breadcrumb":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/insertion-sort-with-a-real-world-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mygreatlearning.com\/blog\/insertion-sort-with-a-real-world-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/insertion-sort-with-a-real-world-example\/#primaryimage","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/iStock-1060938586.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/iStock-1060938586.jpg","width":1302,"height":805,"caption":"Insertion sort"},{"@type":"BreadcrumbList","@id":"https:\/\/www.mygreatlearning.com\/blog\/insertion-sort-with-a-real-world-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/www.mygreatlearning.com\/blog\/"},{"@type":"ListItem","position":2,"name":"IT\/Software Development","item":"https:\/\/www.mygreatlearning.com\/blog\/software\/"},{"@type":"ListItem","position":3,"name":"Insertion Sort with a Real-World Example"}]},{"@type":"WebSite","@id":"https:\/\/www.mygreatlearning.com\/blog\/#website","url":"https:\/\/www.mygreatlearning.com\/blog\/","name":"Great Learning Blog","description":"Learn, Upskill &amp; Career Development Guide and Resources","publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"alternateName":"Great Learning","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mygreatlearning.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization","name":"Great Learning","url":"https:\/\/www.mygreatlearning.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","width":900,"height":900,"caption":"Great Learning"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/GreatLearningOfficial\/","https:\/\/x.com\/Great_Learning","https:\/\/www.instagram.com\/greatlearningofficial\/","https:\/\/www.linkedin.com\/school\/great-learning\/","https:\/\/in.pinterest.com\/greatlearning12\/","https:\/\/www.youtube.com\/user\/beaconelearning\/"],"description":"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.","email":"info@mygreatlearning.com","legalName":"Great Learning Education Services Pvt. Ltd","foundingDate":"2013-11-29","numberOfEmployees":{"@type":"QuantitativeValue","minValue":"1001","maxValue":"5000"}},{"@type":"Person","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad","name":"Great Learning Editorial Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","caption":"Great Learning Editorial Team"},"description":"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.","sameAs":["https:\/\/www.mygreatlearning.com\/","https:\/\/in.linkedin.com\/school\/great-learning\/","https:\/\/x.com\/https:\/\/twitter.com\/Great_Learning","https:\/\/www.youtube.com\/channel\/UCObs0kLIrDjX2LLSybqNaEA"],"award":["Best EdTech Company of the Year 2024","Education Economictimes Outstanding Education\/Edtech Solution Provider of the Year 2024","Leading E-learning Platform 2024"],"url":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"}]}},"uagb_featured_image_src":{"full":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/iStock-1060938586.jpg",1302,805,false],"thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/iStock-1060938586-150x150.jpg",150,150,true],"medium":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/iStock-1060938586-300x185.jpg",300,185,true],"medium_large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/iStock-1060938586-768x475.jpg",768,475,true],"large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/iStock-1060938586-1024x633.jpg",1024,633,true],"1536x1536":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/iStock-1060938586.jpg",1302,805,false],"2048x2048":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/iStock-1060938586.jpg",1302,805,false],"web-stories-poster-portrait":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/iStock-1060938586-640x805.jpg",640,805,true],"web-stories-publisher-logo":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/iStock-1060938586-96x96.jpg",96,96,true],"web-stories-thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/iStock-1060938586-150x93.jpg",150,93,true]},"uagb_author_info":{"display_name":"Great Learning Editorial Team","author_link":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"},"uagb_comment_info":0,"uagb_excerpt":"Insertion Sort is a straightforward algorithm that sorts a list by inserting elements into their correct positions. This guide covers its step-by-step process, code implementation, time complexity analysis, and use cases for small or nearly sorted datasets.","_links":{"self":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/28044","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/comments?post=28044"}],"version-history":[{"count":11,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/28044\/revisions"}],"predecessor-version":[{"id":104925,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/28044\/revisions\/104925"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media\/28055"}],"wp:attachment":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media?parent=28044"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/categories?post=28044"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/tags?post=28044"},{"taxonomy":"content_type","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/content_type?post=28044"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}