{"id":34450,"date":"2024-11-01T09:28:13","date_gmt":"2024-11-01T09:28:13","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34450"},"modified":"2024-11-01T11:41:13","modified_gmt":"2024-11-01T11:41:13","slug":"javascript-coding-test-course-merge-sort","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34450\/","title":{"rendered":"JavaScript Coding Test Course, Merge Sort"},"content":{"rendered":"<div class=\"post\">\n<p>Hello! In this blog post, we will discuss the Merge Sort algorithm to help you prepare for JavaScript coding tests. Merge Sort is one of the most widely used sorting algorithms, with a time complexity of O(n log n) and excellent performance. In this post, we will dramatically explain the concept of Merge Sort, how it works, its implementation in JavaScript, and practical use cases in coding tests.<\/p>\n<h2>What is Merge Sort?<\/h2>\n<p>Merge Sort is an algorithm that uses the divide and conquer method. The basic idea of this algorithm is to recursively divide the array into two sub-arrays, sort each of the sub-arrays, and then merge these two sub-arrays into one sorted array. Merge Sort goes through the following steps:<\/p>\n<ul>\n<li>Divide the array into two sub-arrays based on a midpoint.<\/li>\n<li>Recursively sort each sub-array.<\/li>\n<li>Merge the two sorted sub-arrays to finally create one sorted array.<\/li>\n<\/ul>\n<h2>The Process of Merge Sort<\/h2>\n<p>Let\u2019s take a closer look at how Merge Sort works using an example. Suppose we have an array to sort: [38, 27, 43, 3, 9, 82, 10].<\/p>\n<h3>Step 1: Splitting the Array<\/h3>\n<p>First, let&#8217;s divide the array based on the midpoint. This array can be split into the following two sub-arrays:<\/p>\n<ul>\n<li>[38, 27, 43]<\/li>\n<li>[3, 9, 82, 10]<\/li>\n<\/ul>\n<h3>Step 2: Recursive Sorting<\/h3>\n<p>Now, we repeat the same process for each sub-array. Continuing to split:<\/p>\n<ul>\n<li>[38, 27] -> [38] and [27]<\/li>\n<li>[3, 9, 82, 10] -> [3, 9] and [82, 10] -> [3] and [9], [82] and [10]<\/li>\n<\/ul>\n<h3>Step 3: Merging After Sorting<\/h3>\n<p>Now that each sub-array has been split into single elements, let&#8217;s merge them back while sorting:<\/p>\n<ul>\n<li>Merge [38] and [27] to get [27, 38]<\/li>\n<li>Merge [3] and [9] to get [3, 9]<\/li>\n<li>Merge [82] and [10] to get [10, 82]<\/li>\n<\/ul>\n<p>Now that we have the sorted sub-arrays, let&#8217;s merge them again:<\/p>\n<ul>\n<li>Merge [27, 38] and [3, 9] to get [3, 9, 27, 38]<\/li>\n<li>Merge [3, 9, 27, 38] and [10, 82] to get [3, 9, 10, 27, 38, 82]<\/li>\n<\/ul>\n<p>The final sorted array is [3, 9, 10, 27, 38, 82].<\/p>\n<h2>Time Complexity Analysis of Merge Sort<\/h2>\n<p>The time complexity of Merge Sort is O(n log n). This results from the combination of two factors:<\/p>\n<ul>\n<li>In the process of dividing the array, the size of the array decreases by half, resulting in log n stages.<\/li>\n<li>At each stage, merging the two sub-arrays takes O(n) time.<\/li>\n<\/ul>\n<p>As a result, Merge Sort is widely used as a stable sorting method. However, it has the drawback of relatively high memory usage.<\/p>\n<h2>Implementing Merge Sort in JavaScript<\/h2>\n<p>Now, let&#8217;s implement Merge Sort in JavaScript. Merge Sort fundamentally uses a recursive function. Below is the JavaScript code:<\/p>\n<pre>\n        <code>\nfunction mergeSort(arr) {\n    if (arr.length <= 1) {\n        return arr; \/\/ Base case: return the array as is when it has one element\n    }\n\n    const mid = Math.floor(arr.length \/ 2); \/\/ Mid index of the array\n    const left = mergeSort(arr.slice(0, mid)); \/\/ Recursively sort the left part\n    const right = mergeSort(arr.slice(mid)); \/\/ Recursively sort the right part\n\n    return merge(left, right); \/\/ Merge the two sorted parts\n}\n\nfunction merge(left, right) {\n    const sortedArray = [];\n    let leftIndex = 0; \/\/ Left array index\n    let rightIndex = 0; \/\/ Right array index\n\n    \/\/ Repeat while neither of the arrays is empty\n    while (leftIndex < left.length &#038;&#038; rightIndex < right.length) {\n        if (left[leftIndex] < right[rightIndex]) {\n            sortedArray.push(left[leftIndex]); \/\/ Add the smaller element from the left\n            leftIndex++;\n        } else {\n            sortedArray.push(right[rightIndex]); \/\/ Add the smaller element from the right\n            rightIndex++;\n        }\n    }\n\n    \/\/ Add any remaining elements\n    return sortedArray.concat(left.slice(leftIndex)).concat(right.slice(rightIndex));\n}\n\n\/\/ Test\nconst array = [38, 27, 43, 3, 9, 82, 10];\nconst sortedArray = mergeSort(array);\nconsole.log(sortedArray); \/\/ Output: [3, 9, 10, 27, 38, 43, 82]\n        <\/code>\n    <\/pre>\n<h2>Applications and Cautions of Merge Sort<\/h2>\n<p>Merge Sort is most commonly used when there is a need to sort a large amount of data. It is particularly useful in external sorting (e.g., sorting files). Since Merge Sort is a stable sorting algorithm, it is suitable for cases where the original order must be preserved. However, it consumes a significant amount of memory, so in memory-constrained environments, other algorithms may need to be considered.<\/p>\n<h2>Conclusion<\/h2>\n<p>In this post, we have learned about Merge Sort in detail. From the basic concepts of the algorithm to its implementation, we covered content that can be useful in coding tests. If you can understand and implement Merge Sort well, you will have a solid foundation to easily acquire other algorithms as well. I hope this helps you in your coding test preparation, and I will return with more useful information in the next post. Thank you!<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Hello! In this blog post, we will discuss the Merge Sort algorithm to help you prepare for JavaScript coding tests. Merge Sort is one of the most widely used sorting algorithms, with a time complexity of O(n log n) and excellent performance. In this post, we will dramatically explain the concept of Merge Sort, how &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34450\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;JavaScript Coding Test Course, Merge 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":[141],"tags":[],"class_list":["post-34450","post","type-post","status-publish","format-standard","hentry","category-javascript-coding-test"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>JavaScript Coding Test Course, Merge 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\/34450\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JavaScript Coding Test Course, Merge Sort - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Hello! In this blog post, we will discuss the Merge Sort algorithm to help you prepare for JavaScript coding tests. Merge Sort is one of the most widely used sorting algorithms, with a time complexity of O(n log n) and excellent performance. In this post, we will dramatically explain the concept of Merge Sort, how &hellip; \ub354 \ubcf4\uae30 &quot;JavaScript Coding Test Course, Merge Sort&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34450\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:28:13+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:41:13+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=\"2\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/34450\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34450\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"JavaScript Coding Test Course, Merge Sort\",\"datePublished\":\"2024-11-01T09:28:13+00:00\",\"dateModified\":\"2024-11-01T11:41:13+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34450\/\"},\"wordCount\":539,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Javascript Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34450\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34450\/\",\"name\":\"JavaScript Coding Test Course, Merge Sort - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:28:13+00:00\",\"dateModified\":\"2024-11-01T11:41:13+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34450\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34450\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34450\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript Coding Test Course, Merge 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":"JavaScript Coding Test Course, Merge 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\/34450\/","og_locale":"ko_KR","og_type":"article","og_title":"JavaScript Coding Test Course, Merge Sort - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Hello! In this blog post, we will discuss the Merge Sort algorithm to help you prepare for JavaScript coding tests. Merge Sort is one of the most widely used sorting algorithms, with a time complexity of O(n log n) and excellent performance. In this post, we will dramatically explain the concept of Merge Sort, how &hellip; \ub354 \ubcf4\uae30 \"JavaScript Coding Test Course, Merge Sort\"","og_url":"https:\/\/atmokpo.com\/w\/34450\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:28:13+00:00","article_modified_time":"2024-11-01T11:41:13+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":"2\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/34450\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34450\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"JavaScript Coding Test Course, Merge Sort","datePublished":"2024-11-01T09:28:13+00:00","dateModified":"2024-11-01T11:41:13+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34450\/"},"wordCount":539,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Javascript Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34450\/","url":"https:\/\/atmokpo.com\/w\/34450\/","name":"JavaScript Coding Test Course, Merge Sort - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:28:13+00:00","dateModified":"2024-11-01T11:41:13+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34450\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34450\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34450\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"JavaScript Coding Test Course, Merge 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\/34450","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=34450"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34450\/revisions"}],"predecessor-version":[{"id":34451,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34450\/revisions\/34451"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34450"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34450"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34450"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}