{"id":34822,"date":"2024-11-01T09:32:24","date_gmt":"2024-11-01T09:32:24","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34822"},"modified":"2024-11-01T11:26:23","modified_gmt":"2024-11-01T11:26:23","slug":"swift-coding-test-course-binary-search","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34822\/","title":{"rendered":"Swift Coding Test Course, Binary Search"},"content":{"rendered":"<p><body><\/p>\n<h2>1. Overview of Binary Search<\/h2>\n<p>\n    Binary Search is an algorithm used to find the position of a specific value in a sorted array.<br \/>\n    It is very efficient as it divides the array in half to find the desired value,<br \/>\n    having a time complexity of O(log n) in both average and worst cases.<br \/>\n    This is significantly better than the O(n) of linear search.\n<\/p>\n<h3>1.1 Principle of Binary Search<\/h3>\n<p>\n    Binary Search proceeds with the following steps:\n<\/p>\n<ol>\n<li>Check if the array to be searched is sorted.<\/li>\n<li>Set the start index and end index.<\/li>\n<li>Calculate the middle index.<\/li>\n<li>Compare the middle value to the value you want to find.<\/li>\n<li>If the value to be found is less than the middle value, set the end index to middle index &#8211; 1,<br \/>\n        and if it&#8217;s greater, set the start index to middle index + 1.<\/li>\n<li>Repeat until the value is found or the start index is greater than the end index.<\/li>\n<\/ol>\n<h2>2. Algorithm Problems<\/h2>\n<p>\n    Now, let&#8217;s look at a problem that utilizes binary search.\n<\/p>\n<h3>Problem: Finding the Index of a Specific Number in an Array<\/h3>\n<pre>\nGiven an integer array <code>nums<\/code> and an integer <code>target<\/code>, \nwrite a function that returns the index of <code>target<\/code> if it exists in the array <code>nums<\/code>, \nor -1 if it does not exist.\n\nExample:\nInput: nums = [-1,0,3,5,9,12], target = 9\nOutput: 4\n\nInput: nums = [-1,0,3,5,9,12], target = 2\nOutput: -1\n<\/pre>\n<h3>3. Problem Solving Process<\/h3>\n<p>\n    To solve the problem, we will use the binary search algorithm to find the <code>target<\/code> value in the array.<br \/>\n    I will explain step by step.\n<\/p>\n<h4>3.1 Function Definition<\/h4>\n<p>\n    First, we define the <code>binarySearch<\/code> function that will perform the binary search.<br \/>\n    This function takes the array <code>nums<\/code> and the <code>target<\/code> value as arguments.\n<\/p>\n<pre>\n<code>\nfunc binarySearch(nums: [Int], target: Int) -> Int {\n    var left = 0\n    var right = nums.count - 1\n\n    while left <= right {\n        let mid = left + (right - left) \/ 2\n\n        if nums[mid] == target {\n            return mid\n        } else if nums[mid] < target {\n            left = mid + 1\n        } else {\n            right = mid - 1\n        }\n    }\n    return -1\n}\n<\/code>\n<\/pre>\n<h4>3.2 Variable Initialization<\/h4>\n<p>\n    Initialize the variables <code>left<\/code> and <code>right<\/code> to 0 and the length of the array - 1, respectively.<br \/>\n    <code>left<\/code> represents the starting index of the search range, and <code>right<\/code> represents the ending index.\n<\/p>\n<h4>3.3 Calculating the Middle Value<\/h4>\n<p>\nUse the <code>while<\/code> loop to repeat until <code>left<\/code> is less than or equal to <code>right<\/code>.<br \/>\n    In each iteration, calculate the middle index <code>mid<\/code>.<br \/>\n    When calculating the middle value, use <code>left + (right - left) \/ 2<\/code> to prevent overflow.\n<\/p>\n<h4>3.4 Comparing the Target<\/h4>\n<p>\n    If the middle value <code>nums[mid]<\/code> equals <code>target<\/code>, return that index <code>mid<\/code>.<br \/>\n    If <code>nums[mid]<\/code> is less than <code>target<\/code>,<br \/>\n    set <code>left<\/code> to <code>mid + 1<\/code> to search the right half.<br \/>\n    Conversely, if <code>nums[mid]<\/code> is greater than <code>target<\/code>,<br \/>\n    set <code>right<\/code> to <code>mid - 1<\/code> to search the left half.\n<\/p>\n<h4>3.5 Returning the Result<\/h4>\n<p>\n    When the loop ends, it means <code>target<\/code> does not exist in the array, so return -1.\n<\/p>\n<h3>4. Full Code<\/h3>\n<pre>\n<code>\nfunc binarySearch(nums: [Int], target: Int) -> Int {\n    var left = 0\n    var right = nums.count - 1\n\n    while left <= right {\n        let mid = left + (right - left) \/ 2\n\n        if nums[mid] == target {\n            return mid\n        } else if nums[mid] < target {\n            left = mid + 1\n        } else {\n            right = mid - 1\n        }\n    }\n    return -1\n}\n\n\/\/ Example usage\nlet nums = [-1, 0, 3, 5, 9, 12]\nlet target = 9\nlet result = binarySearch(nums: nums, target: target)\nprint(result) \/\/ 4\n<\/code>\n<\/pre>\n<h2>5. Advantages and Disadvantages of Binary Search<\/h2>\n<h3>5.1 Advantages<\/h3>\n<p>\n    The main advantage of binary search is its fast search speed.<br \/>\n    It shows significantly higher performance compared to linear search when dealing with very large datasets.\n<\/p>\n<h3>5.2 Disadvantages<\/h3>\n<p>\n    However, a disadvantage of using binary search is that the data must be sorted.<br \/>\n    Frequent insertion and deletion of data may require separate sorting operations.\n<\/p>\n<h2>6. Conclusion<\/h2>\n<p>\n    Binary search is an efficient searching method and is one of the topics frequently asked in coding tests.<br \/>\n    Through the above problem, I hope you have understood the principles and implementation methods of binary search,<br \/>\n    and gained useful experience in writing code in Swift.\n<\/p>\n<h2>7. Additional Practice Problems<\/h2>\n<p>\n    To deepen your understanding of binary search, try solving the additional problems below.\n<\/p>\n<ul>\n<li>Write a function that finds and returns all indices of a specific number in a given integer array.<\/li>\n<li>Implement a function that finds the first and last positions in a sorted array.<\/li>\n<li>Write a function that determines whether there is a combination of two numbers in an integer array that adds up to a specific number.<\/li>\n<\/ul>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. Overview of Binary Search Binary Search is an algorithm used to find the position of a specific value in a sorted array. It is very efficient as it divides the array in half to find the desired value, having a time complexity of O(log n) in both average and worst cases. This is significantly &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34822\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Swift Coding Test Course, Binary Search&#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-34822","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, Binary Search - \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\/34822\/\" \/>\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, Binary Search - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"1. Overview of Binary Search Binary Search is an algorithm used to find the position of a specific value in a sorted array. It is very efficient as it divides the array in half to find the desired value, having a time complexity of O(log n) in both average and worst cases. This is significantly &hellip; \ub354 \ubcf4\uae30 &quot;Swift Coding Test Course, Binary Search&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34822\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:32:24+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:26:23+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\/34822\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34822\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Swift Coding Test Course, Binary Search\",\"datePublished\":\"2024-11-01T09:32:24+00:00\",\"dateModified\":\"2024-11-01T11:26:23+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34822\/\"},\"wordCount\":523,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Swift Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34822\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34822\/\",\"name\":\"Swift Coding Test Course, Binary Search - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:32:24+00:00\",\"dateModified\":\"2024-11-01T11:26:23+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34822\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34822\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34822\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Swift Coding Test Course, Binary Search\"}]},{\"@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, Binary Search - \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\/34822\/","og_locale":"ko_KR","og_type":"article","og_title":"Swift Coding Test Course, Binary Search - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"1. Overview of Binary Search Binary Search is an algorithm used to find the position of a specific value in a sorted array. It is very efficient as it divides the array in half to find the desired value, having a time complexity of O(log n) in both average and worst cases. This is significantly &hellip; \ub354 \ubcf4\uae30 \"Swift Coding Test Course, Binary Search\"","og_url":"https:\/\/atmokpo.com\/w\/34822\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:32:24+00:00","article_modified_time":"2024-11-01T11:26:23+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\/34822\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34822\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Swift Coding Test Course, Binary Search","datePublished":"2024-11-01T09:32:24+00:00","dateModified":"2024-11-01T11:26:23+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34822\/"},"wordCount":523,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Swift Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34822\/","url":"https:\/\/atmokpo.com\/w\/34822\/","name":"Swift Coding Test Course, Binary Search - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:32:24+00:00","dateModified":"2024-11-01T11:26:23+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34822\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34822\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34822\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Swift Coding Test Course, Binary Search"}]},{"@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\/34822","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=34822"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34822\/revisions"}],"predecessor-version":[{"id":34823,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34822\/revisions\/34823"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34822"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34822"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34822"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}