{"id":34446,"date":"2024-11-01T09:28:11","date_gmt":"2024-11-01T09:28:11","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34446"},"modified":"2024-11-01T11:41:13","modified_gmt":"2024-11-01T11:41:13","slug":"javascript-coding-test-course-sorting-numbers","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34446\/","title":{"rendered":"JavaScript Coding Test Course, Sorting Numbers"},"content":{"rendered":"<p><body><\/p>\n<h2>Overview<\/h2>\n<p>\n        One of the common problems seen in coding tests is sorting a given number.<br \/>\n        In this course, we will learn how to sort numbers using JavaScript.<br \/>\n        The problem of sorting numbers is very useful for studying basic sorting algorithms.<br \/>\n        We can sort numbers using various algorithms, each with different time and space complexities.\n    <\/p>\n<h2>Problem Description<\/h2>\n<h3>Problem<\/h3>\n<p>\n        Write a function that takes an array of given numbers as input and returns a sorted array.<br \/>\n        For example, if the input array is <code>[3, 1, 2]<\/code>,<br \/>\n        the output should be <code>[1, 2, 3]<\/code>.\n    <\/p>\n<h3>Input<\/h3>\n<ul>\n<li>The first line contains an integer <code>N<\/code>. (<code>1 \u2264 N \u2264 100,000<\/code>)<\/li>\n<li>The second line contains <code>N<\/code> integers <code>A<sub>1<\/sub>, A<sub>2<\/sub>, ..., A<sub>N<\/sub><\/code> separated by a single space.<br \/>\n            (<code>-1,000,000 \u2264 A<sub>i<\/sub> \u2264 1,000,000<\/code>)<\/li>\n<\/ul>\n<h3>Output<\/h3>\n<p>\n        You must print the sorted numbers in one line, separated by a single space.\n    <\/p>\n<h3>Example<\/h3>\n<pre>\n    Input:\n    5\n    5 4 3 2 1\n    \n    Output:\n    1 2 3 4 5\n    <\/pre>\n<h2>Problem Solving Process<\/h2>\n<h3>Step 1: Requirements Analysis<\/h3>\n<p>\n        To solve the given problem, the goal is to sort the input array and output it.<br \/>\n        To do this, we can use classic sorting algorithms such as<br \/>\n        Quick Sort and Merge Sort.<br \/>\n        While we can use built-in JavaScript functions, it is important this time to implement the algorithms ourselves for understanding.\n    <\/p>\n<h3>Step 2: Algorithm Selection<\/h3>\n<p>\n        Among the sorting algorithms, Quick Sort and Merge Sort are generally widely used fast sorting algorithms.<br \/>\n        Below, I will summarize the advantages and disadvantages of each algorithm.\n    <\/p>\n<h4>Quick Sort<\/h4>\n<ul>\n<li>Average time complexity: <code>O(N log N)<\/code><\/li>\n<li>Worst-case time complexity: <code>O(N<sup>2<\/sup>)<\/code> (with inefficient pivot selection)<\/li>\n<li>Space complexity: <code>O(log N)<\/code><\/li>\n<li>Advantages: Can perform in-place sorting with low memory usage.<\/li>\n<li>Disadvantages: It is not a stable sort.<\/li>\n<\/ul>\n<h4>Merge Sort<\/h4>\n<ul>\n<li>Average and worst-case time complexity: <code>O(N log N)<\/code><\/li>\n<li>Space complexity: <code>O(N)<\/code><\/li>\n<li>Advantages: It is a stable sort.<\/li>\n<li>Disadvantages: Requires additional memory.<\/li>\n<\/ul>\n<h3>Step 3: Implementing Quick Sort<\/h3>\n<p>\n        We will implement a function to sort an array of numbers using Quick Sort.<br \/>\n        Below is the implementation code for Quick Sort written in JavaScript.\n    <\/p>\n<pre><code>\n    function quickSort(arr) {\n        if (arr.length <= 1) return arr;\n        const pivot = arr[arr.length - 1];\n        const left = [];\n        const right = [];\n        \n        for (let i = 0; i < arr.length - 1; i++) {\n            if (arr[i] < pivot) {\n                left.push(arr[i]);\n            } else {\n                right.push(arr[i]);\n            }\n        }\n        return [...quickSort(left), pivot, ...quickSort(right)];\n    }\n    <\/code><\/pre>\n<h3>Step 4: Implementing Merge Sort<\/h3>\n<p>\n        Next, let's implement the Merge Sort algorithm. Below is the implementation code for Merge Sort.\n    <\/p>\n<pre><code>\n    function mergeSort(arr) {\n        if (arr.length <= 1) return arr;\n        const mid = Math.floor(arr.length \/ 2);\n        const left = mergeSort(arr.slice(0, mid));\n        const right = mergeSort(arr.slice(mid));\n        \n        return merge(left, right);\n    }\n    \n    function merge(left, right) {\n        const result = [];\n        let i = 0, j = 0;\n        \n        while (i < left.length &#038;&#038; j < right.length) {\n            if (left[i] < right[j]) {\n                result.push(left[i]);\n                i++;\n            } else {\n                result.push(right[j]);\n                j++;\n            }\n        }\n        return result.concat(left.slice(i)).concat(right.slice(j));\n    }\n    <\/code><\/pre>\n<h3>Step 5: Handling Input and Output<\/h3>\n<p>\n        Now that we have completed the sorting functions, we will implement the part that handles input and outputs the result. In JavaScript,<br \/>\n        we can use <code>prompt<\/code> to receive input, and<br \/>\n        the result can be printed using <code>console.log<\/code>.\n    <\/p>\n<pre><code>\n    const N = parseInt(prompt(\"Enter the number of integers:\"));\n    const nums = prompt(\"Enter the integers:\").split(\" \").map(Number);\n    const sorted = quickSort(nums); \/\/ or mergeSort(nums);\n    \n    console.log(sorted.join(\" \"));\n    <\/code><\/pre>\n<h2>Conclusion<\/h2>\n<p>\n        In this course, we solved the problem of sorting a given array of numbers using JavaScript.<br \/>\n        We learned not only the implementation of sorting algorithms but also the characteristics and performance of each algorithm.<br \/>\n        By directly implementing Quick Sort and Merge Sort, we were able to deepen our understanding of sorting algorithms.<br \/>\n        In coding tests, various problems are presented aside from sorting problems, so I recommend practicing these algorithms in various ways.\n    <\/p>\n<h3>Problem Solving and Practice<\/h3>\n<p>\n        Solve the following problems for more practice!\n    <\/p>\n<ul>\n<li>Remove duplicate numbers from the given array and then sort it<\/li>\n<li>Interval sorting: sort only within a given interval<\/li>\n<li>Problem of merging two sorted arrays<\/li>\n<\/ul>\n<h2>References<\/h2>\n<ul>\n<li><a href=\"https:\/\/developer.mozilla.org\/ko\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/sort\">MDN Web Docs: Array.prototype.sort()<\/a><\/li>\n<li><a href=\"https:\/\/ko.wikipedia.org\/wiki\/%EA%B0%84%EB%9E%B5_%EC%A0%95%EB%A6%AC\">Wikipedia: Sorting Algorithms<\/a><\/li>\n<\/ul>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Overview One of the common problems seen in coding tests is sorting a given number. In this course, we will learn how to sort numbers using JavaScript. The problem of sorting numbers is very useful for studying basic sorting algorithms. We can sort numbers using various algorithms, each with different time and space complexities. Problem &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34446\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;JavaScript Coding Test Course, Sorting Numbers&#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-34446","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, Sorting Numbers - \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\/34446\/\" \/>\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, Sorting Numbers - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Overview One of the common problems seen in coding tests is sorting a given number. In this course, we will learn how to sort numbers using JavaScript. The problem of sorting numbers is very useful for studying basic sorting algorithms. We can sort numbers using various algorithms, each with different time and space complexities. Problem &hellip; \ub354 \ubcf4\uae30 &quot;JavaScript Coding Test Course, Sorting Numbers&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34446\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:28:11+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\/34446\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34446\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"JavaScript Coding Test Course, Sorting Numbers\",\"datePublished\":\"2024-11-01T09:28:11+00:00\",\"dateModified\":\"2024-11-01T11:41:13+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34446\/\"},\"wordCount\":468,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Javascript Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34446\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34446\/\",\"name\":\"JavaScript Coding Test Course, Sorting Numbers - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:28:11+00:00\",\"dateModified\":\"2024-11-01T11:41:13+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34446\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34446\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34446\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript Coding Test Course, Sorting Numbers\"}]},{\"@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, Sorting Numbers - \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\/34446\/","og_locale":"ko_KR","og_type":"article","og_title":"JavaScript Coding Test Course, Sorting Numbers - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Overview One of the common problems seen in coding tests is sorting a given number. In this course, we will learn how to sort numbers using JavaScript. The problem of sorting numbers is very useful for studying basic sorting algorithms. We can sort numbers using various algorithms, each with different time and space complexities. Problem &hellip; \ub354 \ubcf4\uae30 \"JavaScript Coding Test Course, Sorting Numbers\"","og_url":"https:\/\/atmokpo.com\/w\/34446\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:28:11+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\/34446\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34446\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"JavaScript Coding Test Course, Sorting Numbers","datePublished":"2024-11-01T09:28:11+00:00","dateModified":"2024-11-01T11:41:13+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34446\/"},"wordCount":468,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Javascript Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34446\/","url":"https:\/\/atmokpo.com\/w\/34446\/","name":"JavaScript Coding Test Course, Sorting Numbers - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:28:11+00:00","dateModified":"2024-11-01T11:41:13+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34446\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34446\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34446\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"JavaScript Coding Test Course, Sorting Numbers"}]},{"@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\/34446","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=34446"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34446\/revisions"}],"predecessor-version":[{"id":34447,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34446\/revisions\/34447"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34446"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34446"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34446"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}