{"id":34234,"date":"2024-11-01T09:25:49","date_gmt":"2024-11-01T09:25:49","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34234"},"modified":"2024-11-01T10:58:04","modified_gmt":"2024-11-01T10:58:04","slug":"c-coding-test-course-sorting-numbers-2-2","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34234\/","title":{"rendered":"C++ Coding Test Course, Sorting Numbers 2"},"content":{"rendered":"<p><body><\/p>\n<h2>Problem Description<\/h2>\n<p>\n        The &#8220;Sorting Numbers 2&#8221; problem is about sorting the given sequence of numbers and outputting it. This problem aims to understand and implement common sorting algorithms.<br \/>\n        Given N numbers, the task is to sort this sequence in ascending order and output the result. The numbers are integers that are greater than or equal to -1000 and less than or equal to 1000.\n    <\/p>\n<h2>Input Format<\/h2>\n<p>\n        The first line contains the number of elements N (1 \u2264 N \u2264 100,000).<br \/>\n        Starting from the second line, N lines will contain the numbers.\n    <\/p>\n<h2>Output Format<\/h2>\n<p>\n        Print the sorted numbers, one per line.\n    <\/p>\n<h2>Example Input<\/h2>\n<pre>\n5\n5\n4\n3\n2\n1\n    <\/pre>\n<h2>Example Output<\/h2>\n<pre>\n1\n2\n3\n4\n5\n    <\/pre>\n<h2>Problem Solving Process<\/h2>\n<h3>1. Understanding the Problem<\/h3>\n<p>\n        This problem is a basic number sorting problem, and various sorting algorithms can be applied.<br \/>\n        In C++, the standard library&#8217;s sorting function can be used, providing an efficient solution.<br \/>\n        However, implementing sorting algorithms manually is also a good practice, so I will introduce two methods: selection sort and insertion sort.\n    <\/p>\n<h3>2. Basic Data Collection and Storage<\/h3>\n<p>\n        To receive input data, an array or vector can be used. In C++, using a vector is common.<br \/>\n        Here is how to store data using a vector.\n    <\/p>\n<pre><code>\n#include &lt;iostream&gt;\n#include &lt;vector&gt;\nusing namespace std;\n\nint main() {\n    int N;\n    cin &gt;&gt; N; \/\/ Input the number of elements\n\n    vector&lt;int&gt; numbers(N);\n    for (int i = 0; i &lt; N; i++) {\n        cin &gt;&gt; numbers[i]; \/\/ Input numbers\n    }\n    return 0;\n}\n    <\/code><\/pre>\n<h3>3. Sorting Algorithms<\/h3>\n<p>\n        Let&#8217;s implement two methods for sorting the vector that contains the input numbers, namely selection sort and insertion sort.\n    <\/p>\n<h4>3-1. Selection Sort<\/h4>\n<p>\n        Selection sort selects the smallest (or largest) element from the given array and moves it to the front. This is repeated until sorting is complete.<br \/>\n        The time complexity of selection sort is O(N^2).\n    <\/p>\n<pre><code>\nvoid selectionSort(vector&lt;int&gt; &amp;arr) {\n    int n = arr.size();\n    for (int i = 0; i &lt; n - 1; i++) {\n        int minIndex = i;\n        for (int j = i + 1; j &lt; n; j++) {\n            if (arr[j] &lt; arr[minIndex]) {\n                minIndex = j;\n            }\n        }\n        swap(arr[i], arr[minIndex]); \/\/ Swap elements\n    }\n}\n    <\/code><\/pre>\n<h4>3-2. Insertion Sort<\/h4>\n<p>\n        Insertion sort inserts each element of the array into its appropriate position to sort the array.<br \/>\n        The time complexity of insertion sort is O(N^2), which is efficient when the data is nearly sorted.\n    <\/p>\n<pre><code>\nvoid insertionSort(vector&lt;int&gt; &amp;arr) {\n    int n = arr.size();\n    for (int i = 1; i &lt; n; i++) {\n        int key = arr[i];\n        int j = i - 1;\n        while (j &gt;= 0 &amp;&amp; arr[j] &gt; key) {\n            arr[j + 1] = arr[j]; \/\/ Move elements\n            j--;\n        }\n        arr[j + 1] = key; \/\/ Insert\n    }\n}\n    <\/code><\/pre>\n<h3>4. Final Code<\/h3>\n<p>Let&#8217;s complete the final code by choosing one of the sorting algorithms above. Below is a method using the vector and the standard library&#8217;s sort function.<\/p>\n<pre><code>\n#include &lt;iostream&gt;\n#include &lt;vector&gt;\n#include &lt;algorithm&gt; \/\/ Includes sort() function\nusing namespace std;\n\nint main() {\n    int N;\n    cin &gt;&gt; N; \/\/ Input the number of elements\n\n    vector&lt;int&gt; numbers(N);\n    for (int i = 0; i &lt; N; i++) {\n        cin &gt;&gt; numbers[i]; \/\/ Input numbers\n    }\n\n    sort(numbers.begin(), numbers.end()); \/\/ Perform sorting\n\n    for (int i = 0; i &lt; N; i++) {\n        cout &lt;&lt; numbers[i] &lt;&lt; endl; \/\/ Print sorted numbers\n    }\n\n    return 0;\n}\n    <\/code><\/pre>\n<h3>5. Time Complexity<\/h3>\n<p>\n        By using the standard library&#8217;s <code>sort<\/code> function as shown above, sorting can be done with an average time complexity of O(N log N).<br \/>\n        This is much more efficient than selection sort and insertion sort.\n    <\/p>\n<h3>6. Conclusion<\/h3>\n<p>\n        Through the &#8220;Sorting Numbers 2&#8221; problem, we practiced the basic concepts of sorting algorithms and how to use C++ vectors.<br \/>\n        Understanding and implementing various sorting algorithms is a very useful process for improving programming skills.<br \/>\n        It will enhance understanding of sorting algorithms commonly used in practice and lay the groundwork for solving more complex algorithms.\n    <\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Description The &#8220;Sorting Numbers 2&#8221; problem is about sorting the given sequence of numbers and outputting it. This problem aims to understand and implement common sorting algorithms. Given N numbers, the task is to sort this sequence in ascending order and output the result. The numbers are integers that are greater than or equal &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34234\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;C++ Coding Test Course, Sorting Numbers 2&#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":[111],"tags":[],"class_list":["post-34234","post","type-post","status-publish","format-standard","hentry","category-c-coding-test-tutorials-2"],"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, Sorting Numbers 2 - \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\/34234\/\" \/>\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, Sorting Numbers 2 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Problem Description The &#8220;Sorting Numbers 2&#8221; problem is about sorting the given sequence of numbers and outputting it. This problem aims to understand and implement common sorting algorithms. Given N numbers, the task is to sort this sequence in ascending order and output the result. The numbers are integers that are greater than or equal &hellip; \ub354 \ubcf4\uae30 &quot;C++ Coding Test Course, Sorting Numbers 2&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34234\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:25:49+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T10:58:04+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\/34234\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34234\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"C++ Coding Test Course, Sorting Numbers 2\",\"datePublished\":\"2024-11-01T09:25:49+00:00\",\"dateModified\":\"2024-11-01T10:58:04+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34234\/\"},\"wordCount\":411,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"C++ Coding Test Tutorials\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34234\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34234\/\",\"name\":\"C++ Coding Test Course, Sorting Numbers 2 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:25:49+00:00\",\"dateModified\":\"2024-11-01T10:58:04+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34234\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34234\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34234\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++ Coding Test Course, Sorting Numbers 2\"}]},{\"@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, Sorting Numbers 2 - \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\/34234\/","og_locale":"ko_KR","og_type":"article","og_title":"C++ Coding Test Course, Sorting Numbers 2 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Problem Description The &#8220;Sorting Numbers 2&#8221; problem is about sorting the given sequence of numbers and outputting it. This problem aims to understand and implement common sorting algorithms. Given N numbers, the task is to sort this sequence in ascending order and output the result. The numbers are integers that are greater than or equal &hellip; \ub354 \ubcf4\uae30 \"C++ Coding Test Course, Sorting Numbers 2\"","og_url":"https:\/\/atmokpo.com\/w\/34234\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:25:49+00:00","article_modified_time":"2024-11-01T10:58:04+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\/34234\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34234\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"C++ Coding Test Course, Sorting Numbers 2","datePublished":"2024-11-01T09:25:49+00:00","dateModified":"2024-11-01T10:58:04+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34234\/"},"wordCount":411,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["C++ Coding Test Tutorials"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34234\/","url":"https:\/\/atmokpo.com\/w\/34234\/","name":"C++ Coding Test Course, Sorting Numbers 2 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:25:49+00:00","dateModified":"2024-11-01T10:58:04+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34234\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34234\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34234\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"C++ Coding Test Course, Sorting Numbers 2"}]},{"@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\/34234","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=34234"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34234\/revisions"}],"predecessor-version":[{"id":34235,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34234\/revisions\/34235"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34234"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34234"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34234"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}