{"id":33279,"date":"2024-11-01T09:15:06","date_gmt":"2024-11-01T09:15:06","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=33279"},"modified":"2024-11-01T11:39:22","modified_gmt":"2024-11-01T11:39:22","slug":"java-coding-test-course-022-sort-an-array-3","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/33279\/","title":{"rendered":"Java Coding Test Course, 022 Sort an Array 3"},"content":{"rendered":"<div class=\"post\">\n<h2>Problem Description<\/h2>\n<p>\n        The problem of sorting a given sequence is a common type of algorithmic problem.<br \/>\n        This problem, titled &#8220;Sorting III&#8221;, requires you to sort an array of integers in ascending order.<br \/>\n        You are given the size N of the input array (1 \u2264 N \u2264 1,000,000) and the elements A[i] of the array (0 \u2264 A[i] \u2264 1,000,000).<br \/>\n        If A[i] is already in sorted form, you can simply output the array without any additional operations.\n    <\/p>\n<h2>Input Format<\/h2>\n<pre>\n        The first line contains the integer N.\n        The second line contains N integers A[i].\n    <\/pre>\n<h2>Output Format<\/h2>\n<p>\n        Output the sorted integer array. Each integer should be separated by a space.\n    <\/p>\n<h2>Example Problem<\/h2>\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>Approach<\/h2>\n<p>\n        To solve this problem, you first need to sort the received array.<br \/>\n        In Java, you can easily sort it using the Arrays.sort() method, but understanding how sorting algorithms work is also important.<br \/>\n        Some sorting algorithms you should learn include Selection Sort, Insertion Sort, Quick Sort, and Merge Sort.\n    <\/p>\n<p>\n        Here, we will use Quick Sort to solve the problem.<br \/>\n        Quick Sort has an average time complexity of O(N log N) and is generally known as one of the most efficient methods for sorting large datasets.\n    <\/p>\n<h2>Algorithm Description<\/h2>\n<ol>\n<li>Generally, Quick Sort works by selecting a pivot and positioning elements smaller than the pivot to the left and those larger to the right.<\/li>\n<li>It sorts by dividing the array and essentially operates through recursive calls.<\/li>\n<li>This process is repeated until all elements are sorted.<\/li>\n<\/ol>\n<h2>Code Implementation<\/h2>\n<pre>\n        <code>\n            import java.util.Arrays;\n\n            public class Main {\n                public static void main(String[] args) {\n                    Scanner scanner = new Scanner(System.in);\n                    int n = scanner.nextInt();\n                    int[] arr = new int[n];\n\n                    for (int i = 0; i &lt; n; i++) {\n                        arr[i] = scanner.nextInt();\n                    }\n\n                    \/\/ Sort the array\n                    Arrays.sort(arr);\n\n                    \/\/ Output the sorted array\n                    for (int num : arr) {\n                        System.out.print(num + \" \");\n                    }\n                }\n            }\n        <\/code>\n    <\/pre>\n<h2>Optimization Methods<\/h2>\n<p>\n        Depending on the case, if the range of array elements is small or there is a specific pattern,<br \/>\n        counting sort may be considered.<br \/>\n        Counting sort has a complexity of O(N + K), where K is the maximum value of the array elements.<br \/>\n        If the distribution of elements is narrow, counting sort can be used to maximize performance.\n    <\/p>\n<p>For example, if the range of numbers is limited to 0 to 1000,<br \/>\n    you can count how many times each value appears between 0 and 1000 and sort based on that.<\/p>\n<h2>Counting Sort Code Implementation<\/h2>\n<pre>\n        <code>\n            import java.util.Scanner;\n\n            public class CountingSort {\n                public static void main(String[] args) {\n                    Scanner scanner = new Scanner(System.in);\n                    int n = scanner.nextInt();\n                    int[] arr = new int[n];\n                    int max = 1000000; \/\/ Maximum value as per problem conditions\n\n                    int[] count = new int[max + 1]; \/\/ Count array with size max + 1\n\n                    \/\/ Receiving input\n                    for (int i = 0; i &lt; n; i++) {\n                        arr[i] = scanner.nextInt();\n                        count[arr[i]]++;\n                    }\n\n                    \/\/ Sort and output\n                    for (int i = 0; i &lt;= max; i++) {\n                        while (count[i] &gt; 0) {\n                            System.out.print(i + \" \");\n                            count[i]--;\n                        }\n                    }\n                }\n            }\n        <\/code>\n    <\/pre>\n<h2>Conclusion<\/h2>\n<p>\n        This &#8220;Sorting III&#8221; problem provides a good opportunity to practice basic understanding of sorting algorithms and array manipulation.<br \/>\n        It is possible to learn various approaches to sorting algorithms through Quick Sort and Counting Sort.<br \/>\n        To solve more complex problems in the future, it\u2019s important to well understand and utilize the basics learned from this problem.\n    <\/p>\n<h2>Additional Problems<\/h2>\n<p>\n        It would also be good practice to think of variations of this problem.<br \/>\n        For example, creating problems like the following can be beneficial:\n    <\/p>\n<ul>\n<li>\n            A problem to track how many times each number appears after randomly inputting numbers from 1 to 1000\n        <\/li>\n<li>\n            A problem of sorting a sequence that can only be sorted at specific intervals\n        <\/li>\n<li>\n            A problem to implement another sorting algorithm, Merge Sort\n        <\/li>\n<\/ul>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem Description The problem of sorting a given sequence is a common type of algorithmic problem. This problem, titled &#8220;Sorting III&#8221;, requires you to sort an array of integers in ascending order. You are given the size N of the input array (1 \u2264 N \u2264 1,000,000) and the elements A[i] of the array (0 &hellip; <a href=\"https:\/\/atmokpo.com\/w\/33279\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Java Coding Test Course, 022 Sort an Array 3&#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":[139],"tags":[],"class_list":["post-33279","post","type-post","status-publish","format-standard","hentry","category-java-coding-test"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Java Coding Test Course, 022 Sort an Array 3 - \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\/33279\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java Coding Test Course, 022 Sort an Array 3 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Problem Description The problem of sorting a given sequence is a common type of algorithmic problem. This problem, titled &#8220;Sorting III&#8221;, requires you to sort an array of integers in ascending order. You are given the size N of the input array (1 \u2264 N \u2264 1,000,000) and the elements A[i] of the array (0 &hellip; \ub354 \ubcf4\uae30 &quot;Java Coding Test Course, 022 Sort an Array 3&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/33279\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:15:06+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:39:22+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\/33279\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33279\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Java Coding Test Course, 022 Sort an Array 3\",\"datePublished\":\"2024-11-01T09:15:06+00:00\",\"dateModified\":\"2024-11-01T11:39:22+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33279\/\"},\"wordCount\":444,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Java Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/33279\/\",\"url\":\"https:\/\/atmokpo.com\/w\/33279\/\",\"name\":\"Java Coding Test Course, 022 Sort an Array 3 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:15:06+00:00\",\"dateModified\":\"2024-11-01T11:39:22+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33279\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/33279\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/33279\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java Coding Test Course, 022 Sort an Array 3\"}]},{\"@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":"Java Coding Test Course, 022 Sort an Array 3 - \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\/33279\/","og_locale":"ko_KR","og_type":"article","og_title":"Java Coding Test Course, 022 Sort an Array 3 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Problem Description The problem of sorting a given sequence is a common type of algorithmic problem. This problem, titled &#8220;Sorting III&#8221;, requires you to sort an array of integers in ascending order. You are given the size N of the input array (1 \u2264 N \u2264 1,000,000) and the elements A[i] of the array (0 &hellip; \ub354 \ubcf4\uae30 \"Java Coding Test Course, 022 Sort an Array 3\"","og_url":"https:\/\/atmokpo.com\/w\/33279\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:15:06+00:00","article_modified_time":"2024-11-01T11:39:22+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\/33279\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/33279\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Java Coding Test Course, 022 Sort an Array 3","datePublished":"2024-11-01T09:15:06+00:00","dateModified":"2024-11-01T11:39:22+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/33279\/"},"wordCount":444,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Java Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/33279\/","url":"https:\/\/atmokpo.com\/w\/33279\/","name":"Java Coding Test Course, 022 Sort an Array 3 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:15:06+00:00","dateModified":"2024-11-01T11:39:22+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/33279\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/33279\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/33279\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Java Coding Test Course, 022 Sort an Array 3"}]},{"@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\/33279","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=33279"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33279\/revisions"}],"predecessor-version":[{"id":33280,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33279\/revisions\/33280"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=33279"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=33279"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=33279"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}