{"id":33976,"date":"2024-11-01T09:22:36","date_gmt":"2024-11-01T09:22:36","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=33976"},"modified":"2024-11-01T10:54:39","modified_gmt":"2024-11-01T10:54:39","slug":"c-coding-test-course-insertion-sort","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/33976\/","title":{"rendered":"C# Coding Test Course, Insertion Sort"},"content":{"rendered":"<p><body><\/p>\n<article>\n<section>\n<h2>Introduction<\/h2>\n<p>Hello. In this lecture, we will take a deep dive into one of the useful sorting algorithms for preparing coding tests, called &#8216;Insertion Sort&#8217;. Sorting algorithms are an important topic that forms the foundation for data structures and algorithm problem-solving. In particular, we aim to help readers understand by presenting theoretical background, code implementation, and specific examples of problem-solving.<\/p>\n<h2>What is Insertion Sort?<\/h2>\n<p>Insertion Sort is a sorting algorithm that works by inserting new data into an already sorted state. It selects one element at a time from the given data and inserts it into the appropriate position by comparing it with the already sorted portion. This algorithm is very intuitive and easy to implement.<\/p>\n<h2>How Insertion Sort Works<\/h2>\n<p>Insertion Sort works in the following way:<\/p>\n<ol>\n<li>Select the first element from the unsorted list. This element is considered to be already sorted.<\/li>\n<li>Select the next element and compare it with the sorted elements.<\/li>\n<li>If the currently selected element is smaller than the sorted element, insert it into the appropriate position in the sorted list.<\/li>\n<li>Repeat this process until the end of the list.<\/li>\n<\/ol>\n<h2>Time Complexity of Insertion Sort<\/h2>\n<p>The time complexity of Insertion Sort is O(n\u00b2) in the worst case. However, if the data is nearly sorted, it shows an average performance of O(n). This can be an advantage of Insertion Sort compared to other algorithms. In special cases, it operates in O(n), making it frequently used in real problems.<\/p>\n<h2>C# Implementation of Insertion Sort<\/h2>\n<p>Now, let&#8217;s implement the Insertion Sort algorithm using C#:<\/p>\n<pre><code>\nusing System;\n\nclass Program\n{\n    static void InsertionSort(int[] arr)\n    {\n        for (int i = 1; i &lt; arr.Length; i++)\n        {\n            int key = arr[i];\n            int j = i - 1;\n\n            \/\/ Move elements that are greater than the current key one position back\n            while (j &gt;= 0 &amp;&amp; arr[j] &gt; key)\n            {\n                arr[j + 1] = arr[j];\n                j--;\n            }\n            arr[j + 1] = key; \/\/ Insert the key in the appropriate position\n        }\n    }\n\n    static void Main()\n    {\n        int[] array = { 12, 11, 13, 5, 6 };\n        InsertionSort(array);\n\n        Console.WriteLine(\"Sorted array: \");\n        foreach (int num in array)\n        {\n            Console.Write(num + \" \");\n        }\n    }\n}\n<\/code><\/pre>\n<h2>Problem Solving: Sorting Given Array<\/h2>\n<p>Here is an example of a problem that may be given in a coding test:<\/p>\n<blockquote>\n<p><strong>Problem:<\/strong> Given an integer array, use insertion sort to sort the array in ascending order.<\/p>\n<p><strong>Input Example:<\/strong> [64, 34, 25, 12, 22, 11, 90]<\/p>\n<p><strong>Output Example:<\/strong> [11, 12, 22, 25, 34, 64, 90]<\/p>\n<\/blockquote>\n<p>To solve the above problem, we can implement the Insertion Sort algorithm as previously described. Let\u2019s sort the given array using insertion sort.<\/p>\n<pre><code>\nusing System;\n\nclass InsertionSortExample\n{\n    static void InsertSort(int[] array)\n    {\n        for (int i = 1; i &lt; array.Length; i++)\n        {\n            int key = array[i];\n            int j = i - 1;\n\n            while (j &gt;= 0 &amp;&amp; array[j] &gt; key)\n            {\n                array[j + 1] = array[j];\n                j--;\n            }\n            array[j + 1] = key;\n        }\n    }\n\n    static void Main()\n    {\n        int[] nums = { 64, 34, 25, 12, 22, 11, 90 };\n        InsertSort(nums);\n\n        Console.WriteLine(\"Sorted array: \");\n        foreach (var num in nums)\n        {\n            Console.Write(num + \" \");\n        }\n    }\n}\n<\/code><\/pre>\n<h2>Conclusion<\/h2>\n<p>In this lecture, we have deeply understood the Insertion Sort algorithm, how to implement it in C#, and the process of solving actual problems. Due to its relatively easy understanding and implementation, Insertion Sort is often used as the basis for many coding test problems. Now that you have mastered Insertion Sort, try solving various problems to further improve your skills!<\/p>\n<\/section>\n<\/article>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Hello. In this lecture, we will take a deep dive into one of the useful sorting algorithms for preparing coding tests, called &#8216;Insertion Sort&#8217;. Sorting algorithms are an important topic that forms the foundation for data structures and algorithm problem-solving. In particular, we aim to help readers understand by presenting theoretical background, code implementation, &hellip; <a href=\"https:\/\/atmokpo.com\/w\/33976\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;C# Coding Test Course, Insertion Sort&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[90],"tags":[],"class_list":["post-33976","post","type-post","status-publish","format-standard","hentry","category-c-coding-test-tutorials"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>C# Coding Test Course, Insertion Sort - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/33976\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C# Coding Test Course, Insertion Sort - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Introduction Hello. In this lecture, we will take a deep dive into one of the useful sorting algorithms for preparing coding tests, called &#8216;Insertion Sort&#8217;. Sorting algorithms are an important topic that forms the foundation for data structures and algorithm problem-solving. In particular, we aim to help readers understand by presenting theoretical background, code implementation, &hellip; \ub354 \ubcf4\uae30 &quot;C# Coding Test Course, Insertion Sort&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/33976\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:22:36+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T10:54:39+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"3\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/33976\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33976\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"C# Coding Test Course, Insertion Sort\",\"datePublished\":\"2024-11-01T09:22:36+00:00\",\"dateModified\":\"2024-11-01T10:54:39+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33976\/\"},\"wordCount\":390,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"C# Coding Test Tutorials\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/33976\/\",\"url\":\"https:\/\/atmokpo.com\/w\/33976\/\",\"name\":\"C# Coding Test Course, Insertion Sort - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:22:36+00:00\",\"dateModified\":\"2024-11-01T10:54:39+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33976\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/33976\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/33976\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C# Coding Test Course, Insertion Sort\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"C# Coding Test Course, Insertion Sort - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","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:\/\/atmokpo.com\/w\/33976\/","og_locale":"ko_KR","og_type":"article","og_title":"C# Coding Test Course, Insertion Sort - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Introduction Hello. In this lecture, we will take a deep dive into one of the useful sorting algorithms for preparing coding tests, called &#8216;Insertion Sort&#8217;. Sorting algorithms are an important topic that forms the foundation for data structures and algorithm problem-solving. In particular, we aim to help readers understand by presenting theoretical background, code implementation, &hellip; \ub354 \ubcf4\uae30 \"C# Coding Test Course, Insertion Sort\"","og_url":"https:\/\/atmokpo.com\/w\/33976\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:22:36+00:00","article_modified_time":"2024-11-01T10:54:39+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"3\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/33976\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/33976\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"C# Coding Test Course, Insertion Sort","datePublished":"2024-11-01T09:22:36+00:00","dateModified":"2024-11-01T10:54:39+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/33976\/"},"wordCount":390,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["C# Coding Test Tutorials"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/33976\/","url":"https:\/\/atmokpo.com\/w\/33976\/","name":"C# Coding Test Course, Insertion Sort - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:22:36+00:00","dateModified":"2024-11-01T10:54:39+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/33976\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/33976\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/33976\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"C# Coding Test Course, Insertion Sort"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33976","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=33976"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33976\/revisions"}],"predecessor-version":[{"id":33977,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33976\/revisions\/33977"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=33976"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=33976"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=33976"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}