{"id":34760,"date":"2024-11-01T09:31:42","date_gmt":"2024-11-01T09:31:42","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34760"},"modified":"2024-11-01T11:26:39","modified_gmt":"2024-11-01T11:26:39","slug":"swift-coding-test-course-selection-sort","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34760\/","title":{"rendered":"Swift Coding Test Course, Selection Sort"},"content":{"rendered":"<p><body><\/p>\n<p>Hello! In this blog, I will conduct a coding test preparation algorithm lecture for developers using Swift. The topic is &#8216;Selection Sort&#8217;. Selection Sort is a sorting algorithm that helps to understand the basic concepts of algorithms. In this article, I will explain the definition of selection sort, how it works, how to implement it in Swift, and how to solve problems using it in detail.<\/p>\n<h2>1. What is Selection Sort?<\/h2>\n<p>Selection sort is one of the simple sorting algorithms, which sorts a given list by finding the smallest (or largest) element and swapping it with the first element of the list. Selection sort performs sorting through the process of repeatedly selecting from the entire list.<\/p>\n<h3>How Selection Sort Works<\/h3>\n<ol>\n<li>Find the smallest value in the list.<\/li>\n<li>Swap the found value with the first element of the list.<\/li>\n<li>Find and swap the smallest value again from the rest of the list, excluding the first element.<\/li>\n<li>Repeat this process until the list is sorted.<\/li>\n<\/ol>\n<h2>2. Time Complexity of Selection Sort<\/h2>\n<p>The time complexity of selection sort is O(n<sup>2<\/sup>). This is due to the operation of two nested loops. Selection sort exhibits a performance of O(n<sup>2<\/sup>) in both the best and worst case. Therefore, it can become inefficient as the number of data increases.<\/p>\n<h2>3. Implementing in Swift<\/h2>\n<p>Now let&#8217;s implement selection sort in Swift. Below is an example of a function that sorts an array using the selection sort method:<\/p>\n<pre>\n<code>\nfunc selectionSort(_ array: inout [Int]) {\n    let count = array.count\n    for i in 0..<count {\n        var minIndex = i\n        for j in (i + 1)..<count {\n            if array[j] < array[minIndex] {\n                minIndex = j\n            }\n        }\n        if minIndex != i {\n            array.swapAt(i, minIndex)\n        }\n    }\n}\n<\/code>\n<\/pre>\n<h3>3.1 Explanation<\/h3>\n<p>Here is a detailed explanation of the functionality of this code:<\/p>\n<ol>\n<li><code>func selectionSort(_ array: inout [Int]) {<\/code>: Defines the selection sort function and takes the array to be sorted as input. The <code>inout<\/code> keyword allows modification of the array within the function.<\/li>\n<li><code>let count = array.count<\/code>: Stores the number of elements in the array.<\/li>\n<li><code>for i in 0..<count {<\/code>: Iterates through the array indices one by one. In selection sort, the minimum value is found for each position.<\/li>\n<li><code>var minIndex = i<\/code>: Initializes the current index <code>i<\/code> as the minimum value index.<\/li>\n<li><code>for j in (i + 1)..<count {<\/code>: Iterates through the indices from <code>i + 1<\/code> to <code>count<\/code> to find the minimum value.<\/li>\n<li><code>if array[j] &lt; array[minIndex] { minIndex = j }<\/code>: Updates <code>minIndex<\/code> if the current value being compared is smaller than the previous minimum value.<\/li>\n<li><code>if minIndex != i { array.swapAt(i, minIndex) }<\/code>: Swaps the two values if the current minimum value's position is different from <code>i<\/code>.<\/li>\n<\/ol>\n<h2>4. Algorithm Problem Utilizing Selection Sort<\/h2>\n<p>Now, I will introduce an actual problem where selection sort can be applied.<\/p>\n<h3>Problem: Sort an Array of Integers<\/h3>\n<p>Given an array of integers, use selection sort to sort the array in ascending order.<\/p>\n<h3>Function Definition<\/h3>\n<p>Let's look at the definition of a Swift function that performs the given functionality.<\/p>\n<pre>\n<code>\nfunc sortArrayWithSelectionSort(array: [Int]) -> [Int] {\n    var sortedArray = array\n    selectionSort(&sortedArray)\n    return sortedArray\n}\n<\/code>\n<\/pre>\n<h3>5. How to Use the Selection Sort Function<\/h3>\n<p>Here is how to use the selection sort function to sort an array.<\/p>\n<pre>\n<code>\nlet unsortedArray = [64, 25, 12, 22, 11]\nlet sortedArray = sortArrayWithSelectionSort(array: unsortedArray)\nprint(sortedArray)  \/\/ Output: [11, 12, 22, 25, 64]\n<\/code>\n<\/pre>\n<h3>6. Conclusion<\/h3>\n<p>In this lecture, we explored the principles of the selection sort algorithm and how to implement it in Swift. While selection sort is not highly complex by itself, it is not the most efficient sorting method in practice. However, by understanding its simple principles, you should have been able to grasp the flow of basic algorithms.<\/p>\n<p>I hope that with further study of various sorting methods and a better understanding of more complex algorithms and data structures, you will perform excellently in interviews and coding tests.<\/p>\n<h2>References<\/h2>\n<ul>\n<li><a href=\"https:\/\/ko.wikipedia.org\/wiki\/%EC%84%A0%ED%83%9D_%EC%A0%95%EB%A0%AC\">Selection Sort - Wikipedia<\/a><\/li>\n<li><a href=\"https:\/\/www.geeksforgeeks.org\/selection-sort\/\">Selection Sort - GeeksforGeeks<\/a><\/li>\n<li><a href=\"https:\/\/www.programiz.com\/dsa\/selection-sort\">Selection Sort - Programiz<\/a><\/li>\n<\/ul>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello! In this blog, I will conduct a coding test preparation algorithm lecture for developers using Swift. The topic is &#8216;Selection Sort&#8217;. Selection Sort is a sorting algorithm that helps to understand the basic concepts of algorithms. In this article, I will explain the definition of selection sort, how it works, how to implement it &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34760\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Swift 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":[129],"tags":[],"class_list":["post-34760","post","type-post","status-publish","format-standard","hentry","category-swift-coding-test"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Swift 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\/34760\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Swift Coding Test Course, Selection Sort - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Hello! In this blog, I will conduct a coding test preparation algorithm lecture for developers using Swift. The topic is &#8216;Selection Sort&#8217;. Selection Sort is a sorting algorithm that helps to understand the basic concepts of algorithms. In this article, I will explain the definition of selection sort, how it works, how to implement it &hellip; \ub354 \ubcf4\uae30 &quot;Swift Coding Test Course, Selection Sort&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34760\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:31:42+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:26:39+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\/34760\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34760\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Swift Coding Test Course, Selection Sort\",\"datePublished\":\"2024-11-01T09:31:42+00:00\",\"dateModified\":\"2024-11-01T11:26:39+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34760\/\"},\"wordCount\":532,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Swift Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34760\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34760\/\",\"name\":\"Swift Coding Test Course, Selection Sort - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:31:42+00:00\",\"dateModified\":\"2024-11-01T11:26:39+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34760\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34760\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34760\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Swift 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":"Swift 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\/34760\/","og_locale":"ko_KR","og_type":"article","og_title":"Swift Coding Test Course, Selection Sort - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Hello! In this blog, I will conduct a coding test preparation algorithm lecture for developers using Swift. The topic is &#8216;Selection Sort&#8217;. Selection Sort is a sorting algorithm that helps to understand the basic concepts of algorithms. In this article, I will explain the definition of selection sort, how it works, how to implement it &hellip; \ub354 \ubcf4\uae30 \"Swift Coding Test Course, Selection Sort\"","og_url":"https:\/\/atmokpo.com\/w\/34760\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:31:42+00:00","article_modified_time":"2024-11-01T11:26:39+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\/34760\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34760\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Swift Coding Test Course, Selection Sort","datePublished":"2024-11-01T09:31:42+00:00","dateModified":"2024-11-01T11:26:39+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34760\/"},"wordCount":532,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Swift Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34760\/","url":"https:\/\/atmokpo.com\/w\/34760\/","name":"Swift Coding Test Course, Selection Sort - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:31:42+00:00","dateModified":"2024-11-01T11:26:39+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34760\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34760\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34760\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Swift 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\/34760","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=34760"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34760\/revisions"}],"predecessor-version":[{"id":34761,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34760\/revisions\/34761"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34760"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34760"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34760"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}