{"id":34220,"date":"2024-11-01T09:25:40","date_gmt":"2024-11-01T09:25:40","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34220"},"modified":"2024-11-01T10:58:07","modified_gmt":"2024-11-01T10:58:07","slug":"c-coding-test-course-selection-sort-2","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34220\/","title":{"rendered":"C++ Coding Test Course, Selection Sort"},"content":{"rendered":"<p><body><\/p>\n<h2>Problem Description<\/h2>\n<p>Write a function to sort a given integer array in ascending order. The sorting algorithm to be used is Selection Sort.<\/p>\n<h3>Input<\/h3>\n<ul>\n<li>The first line contains an integer <code>N<\/code>. (1 \u2264 N \u2264 1000)<\/li>\n<li>The second line contains <code>N<\/code> integers. (Integer <code>A[i]<\/code> is -10000 \u2264 <code>A[i]<\/code> \u2264 10000)<\/li>\n<\/ul>\n<h3>Output<\/h3>\n<p>Output the sorted array in ascending order in a single line, separated by spaces.<\/p>\n<h2>Concept Review of Selection Sort<\/h2>\n<p>The selection sort is one of the simplest sorting algorithms. It works by finding the smallest value in the given list and swapping it with the value at the front. This process is repeated until the list is sorted.<\/p>\n<h3>Process of Selection Sort<\/h3>\n<ol>\n<li>Find the smallest value in the current list.<\/li>\n<li>Swap that value with the value at the current position.<\/li>\n<li>Repeat steps 1 and 2 to sort the list.<\/li>\n<\/ol>\n<h2>Problem-Solving Process<\/h2>\n<h3>1. Understanding for Problem Solving<\/h3>\n<p>To solve the problem, we start by finding the smallest value in the array and placing it at the front, then we find the next smallest value and place it in the second position. This process is repeated for the length of the array, and we need to search the remainder of the array to find the smallest value at each step.<\/p>\n<h3>2. Implementation of Selection Sort Algorithm<\/h3>\n<p>Now let&#8217;s implement the selection sort algorithm in C++. First, we will take the input for the array, then apply the selection sort algorithm to sort it, and finally print the result.<\/p>\n<pre><code>\n#include &lt;iostream&gt;\nusing namespace std;\n\nvoid selectionSort(int arr[], int n) {\n    for (int i = 0; i &lt; n - 1; i++) {\n        \/\/ Set the current position as the index of the minimum value\n        int minIndex = i;\n        for (int j = i + 1; j &lt; n; j++) {\n            if (arr[j] &lt; arr[minIndex]) {\n                minIndex = j; \/\/ If a minimum value is found\n            }\n        }\n        \/\/ Swap the current position with the minimum value's position\n        int temp = arr[i];\n        arr[i] = arr[minIndex];\n        arr[minIndex] = temp;\n    }\n}\n\nint main() {\n    int n;\n    cin &gt;&gt; n;\n    int arr[n];\n    for(int i = 0; i &lt; n; i++) {\n        cin &gt;&gt; arr[i];\n    }\n\n    selectionSort(arr, n);\n\n    for(int i = 0; i &lt; n; i++) {\n        cout &lt;&lt; arr[i];\n        if (i &lt; n - 1) cout &lt;&lt; \" \";\n    }\n    cout &lt;&lt; endl;\n\n    return 0;\n}\n    <\/code><\/pre>\n<h3>3. Code Explanation<\/h3>\n<p>Let me explain each part of the code.<\/p>\n<ul>\n<li><code>void selectionSort(int arr[], int n)<\/code>: This function performs selection sort. It takes an array and its size as parameters.<\/li>\n<li><code>for (int i = 0; i &lt; n - 1; i++)<\/code>: Iterates through the array to perform sorting.<\/li>\n<li><code>int minIndex = i;<\/code>: Initializes the index of the minimum value at the current index.<\/li>\n<li><code>if (arr[j] &lt; arr[minIndex])<\/code>: Updates the new minimum value&#8217;s index if the current value is smaller than the minimum value.<\/li>\n<li><code>int temp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = temp;<\/code>: Swaps the value at the current index with the value at the minimum value&#8217;s index.<\/li>\n<\/ul>\n<h3>4. Example Execution<\/h3>\n<p>Below is an example execution of the selection sort algorithm. First, we input <code>N<\/code> and the array values.<\/p>\n<pre><code>\nInput:\n5\n64 25 12 22 11\n\nOutput:\n11 12 22 25 64\n    <\/code><\/pre>\n<h2>Time Complexity<\/h2>\n<p>The time complexity of selection sort is <code>O(N<sup>2<\/sup>)<\/code>. It requires two nested loops since we need to traverse all the elements to find the minimum value. Thus, performance can degrade with a large amount of data.<\/p>\n<h2>Conclusion<\/h2>\n<p>In this post, we implemented selection sort using C++ and learned the basic concepts of sorting algorithms. Although selection sort is simple, it is not very efficient, so we should consider other sorting algorithms for larger datasets.<\/p>\n<h2>Additional Resources<\/h2>\n<p>Many sorting algorithms exist beyond selection sort. It is recommended to learn the following algorithms:<\/p>\n<ul>\n<li>Bubble Sort<\/li>\n<li>Insertion Sort<\/li>\n<li>Merge Sort<\/li>\n<li>Quick Sort<\/li>\n<\/ul>\n<p>If you want to learn more about algorithms and data structures, please refer to the following resources:<\/p>\n<ul>\n<li><a href=\"https:\/\/www.geeksforgeeks.org\/fundamentals-of-algorithms\/\">Geeks for Geeks &#8211; Fundamentals of Algorithms<\/a><\/li>\n<li><a href=\"https:\/\/www.coursera.org\/learn\/algorithm-design-analysis\">Coursera &#8211; Algorithm Design and Analysis<\/a><\/li>\n<\/ul>\n<footer>\n<p>\u00a9 2023 My Coding Blog. All rights reserved.<\/p>\n<\/footer>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Description Write a function to sort a given integer array in ascending order. The sorting algorithm to be used is Selection Sort. Input The first line contains an integer N. (1 \u2264 N \u2264 1000) The second line contains N integers. (Integer A[i] is -10000 \u2264 A[i] \u2264 10000) Output Output the sorted array &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34220\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;C++ Coding Test Course, Selection 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":[111],"tags":[],"class_list":["post-34220","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, Selection 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\/34220\/\" \/>\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, Selection Sort - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Problem Description Write a function to sort a given integer array in ascending order. The sorting algorithm to be used is Selection Sort. Input The first line contains an integer N. (1 \u2264 N \u2264 1000) The second line contains N integers. (Integer A[i] is -10000 \u2264 A[i] \u2264 10000) Output Output the sorted array &hellip; \ub354 \ubcf4\uae30 &quot;C++ Coding Test Course, Selection Sort&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34220\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:25:40+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T10:58:07+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\/34220\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34220\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"C++ Coding Test Course, Selection Sort\",\"datePublished\":\"2024-11-01T09:25:40+00:00\",\"dateModified\":\"2024-11-01T10:58:07+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34220\/\"},\"wordCount\":469,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"C++ Coding Test Tutorials\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34220\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34220\/\",\"name\":\"C++ Coding Test Course, Selection Sort - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:25:40+00:00\",\"dateModified\":\"2024-11-01T10:58:07+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34220\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34220\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34220\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++ Coding Test Course, Selection 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, Selection 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\/34220\/","og_locale":"ko_KR","og_type":"article","og_title":"C++ Coding Test Course, Selection Sort - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Problem Description Write a function to sort a given integer array in ascending order. The sorting algorithm to be used is Selection Sort. Input The first line contains an integer N. (1 \u2264 N \u2264 1000) The second line contains N integers. (Integer A[i] is -10000 \u2264 A[i] \u2264 10000) Output Output the sorted array &hellip; \ub354 \ubcf4\uae30 \"C++ Coding Test Course, Selection Sort\"","og_url":"https:\/\/atmokpo.com\/w\/34220\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:25:40+00:00","article_modified_time":"2024-11-01T10:58:07+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\/34220\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34220\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"C++ Coding Test Course, Selection Sort","datePublished":"2024-11-01T09:25:40+00:00","dateModified":"2024-11-01T10:58:07+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34220\/"},"wordCount":469,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["C++ Coding Test Tutorials"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34220\/","url":"https:\/\/atmokpo.com\/w\/34220\/","name":"C++ Coding Test Course, Selection Sort - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:25:40+00:00","dateModified":"2024-11-01T10:58:07+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34220\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34220\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34220\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"C++ Coding Test Course, Selection 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\/34220","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=34220"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34220\/revisions"}],"predecessor-version":[{"id":34221,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34220\/revisions\/34221"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34220"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34220"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}