{"id":34068,"date":"2024-11-01T09:23:46","date_gmt":"2024-11-01T09:23:46","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34068"},"modified":"2024-11-01T10:53:33","modified_gmt":"2024-11-01T10:53:33","slug":"c-coding-test-course-binary-search","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34068\/","title":{"rendered":"C# Coding Test Course, Binary Search"},"content":{"rendered":"<p><body><\/p>\n<h2>1. Problem Definition<\/h2>\n<p>\n        Binary search is an algorithm for finding a specific value in a sorted array. It works by comparing the middle value of the array and halving the search range.<br \/>\n        The time complexity of binary search is O(log n), making it very efficient. Let&#8217;s understand the principles of this algorithm by implementing it in practice.\n    <\/p>\n<h2>2. Problem Description<\/h2>\n<p>\n        The following problem involves finding a specific number using binary search:\n    <\/p>\n<pre>\n        <code>\n            \/\/ Problem: Find a specific number in the given integer array.\n            \/\/ The array is sorted in ascending order.\n            \/\/ If the number is found, return its index, \n            \/\/ and return -1 if it is not found.\n        <\/code>\n    <\/pre>\n<h3>Example Input<\/h3>\n<ul>\n<li>Input Array: [2, 3, 4, 10, 40]<\/li>\n<li>Value to Find: 10<\/li>\n<\/ul>\n<h3>Example Output<\/h3>\n<ul>\n<li>Output: 3 (10 is located at index 3)<\/li>\n<\/ul>\n<h2>3. Problem Solving Strategy<\/h2>\n<p>\n        The basic idea of binary search is as follows:\n    <\/p>\n<ul>\n<li>Select the middle element of the array.<\/li>\n<li>Compare the middle element with the value to be found.<\/li>\n<li>If the value to be found is less than the middle element, reduce the search range to the left half of the array.<\/li>\n<li>If the value to be found is greater than the middle element, reduce the search range to the right half of the array.<\/li>\n<li>Repeat this process until the value to be found is found.<\/li>\n<\/ul>\n<h2>4. Algorithm Implementation<\/h2>\n<p>\n        Now let&#8217;s implement the binary search algorithm in C#. The following code is an implementation of the binary search algorithm that finds a specific value in the given array.\n    <\/p>\n<pre>\n        <code>\n            using System;\n\n            class Program\n            {\n                static int BinarySearch(int[] arr, int target)\n                {\n                    int left = 0;\n                    int right = arr.Length - 1;\n\n                    while (left <= right)\n                    {\n                        int mid = left + (right - left) \/ 2;\n\n                        \/\/ Compare mid value with the target value\n                        if (arr[mid] == target)\n                        {\n                            return mid; \/\/ Return index if the value is found\n                        }\n                        else if (arr[mid] < target)\n                        {\n                            left = mid + 1; \/\/ Search in the right half of the array\n                        }\n                        else\n                        {\n                            right = mid - 1; \/\/ Search in the left half of the array\n                        }\n                    }\n\n                    return -1; \/\/ Case when the value is not found\n                }\n\n                static void Main(string[] args)\n                {\n                    int[] arr = { 2, 3, 4, 10, 40 };\n                    int target = 10;\n                    int result = BinarySearch(arr, target);\n\n                    if (result == -1)\n                    {\n                        Console.WriteLine(\"The value to be found does not exist.\");\n                    }\n                    else\n                    {\n                        Console.WriteLine(\"Index of the value to be found: \" + result);\n                    }\n                }\n            }\n        <\/code>\n    <\/pre>\n<h2>5. Code Explanation<\/h2>\n<p>\n        Let's take a closer look at how binary search is implemented in the C# code above.\n    <\/p>\n<ul>\n<li><code>BinarySearch<\/code> method takes two parameters: an integer array <code>arr<\/code> and the value to be found <code>target<\/code>.<\/li>\n<li>The variable <code>left<\/code> stores the starting index of the array, and <code>right<\/code> stores the ending index of the array.<\/li>\n<li>The loop <code>while (left <= right)<\/code> continues as long as there is a search range left.<\/li>\n<li>The middle index is calculated as <code>int mid = left + (right - left) \/ 2;<\/code>. This is one way to prevent index overflow in large arrays.<\/li>\n<li>By comparing the mid value with the target value, we modify the value of <code>left<\/code> or <code>right<\/code> based on the conditions.<\/li>\n<li>If the target value is found, the corresponding index is returned, and -1 is returned if the target value is not found.<\/li>\n<\/ul>\n<h2>6. Time Complexity<\/h2>\n<p>\n        The time complexity of binary search is O(log n). This is because the data is halved during the search process.<br \/>\n        Even if n is a very large number, binary search can derive the result with relatively few comparisons.\n    <\/p>\n<h2>7. Conclusion<\/h2>\n<p>\n        The binary search algorithm operates very efficiently when the data is sorted.<br \/>\n        By understanding and implementing this algorithm well, it will be very helpful in various coding tests and development tasks.<br \/>\n        I hope you enhance your skills in binary search by solving algorithm problems.\n    <\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. Problem Definition Binary search is an algorithm for finding a specific value in a sorted array. It works by comparing the middle value of the array and halving the search range. The time complexity of binary search is O(log n), making it very efficient. Let&#8217;s understand the principles of this algorithm by implementing it &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34068\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;C# 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":[90],"tags":[],"class_list":["post-34068","post","type-post","status-publish","format-standard","hentry","category-c-coding-test-tutorials"],"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, 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\/34068\/\" \/>\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, Binary Search - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"1. Problem Definition Binary search is an algorithm for finding a specific value in a sorted array. It works by comparing the middle value of the array and halving the search range. The time complexity of binary search is O(log n), making it very efficient. Let&#8217;s understand the principles of this algorithm by implementing it &hellip; \ub354 \ubcf4\uae30 &quot;C# Coding Test Course, Binary Search&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34068\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:23:46+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T10:53:33+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=\"1\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/34068\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34068\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"C# Coding Test Course, Binary Search\",\"datePublished\":\"2024-11-01T09:23:46+00:00\",\"dateModified\":\"2024-11-01T10:53:33+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34068\/\"},\"wordCount\":414,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"C# Coding Test Tutorials\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34068\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34068\/\",\"name\":\"C# Coding Test Course, Binary Search - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:23:46+00:00\",\"dateModified\":\"2024-11-01T10:53:33+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34068\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34068\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34068\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C# 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":"C# 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\/34068\/","og_locale":"ko_KR","og_type":"article","og_title":"C# Coding Test Course, Binary Search - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"1. Problem Definition Binary search is an algorithm for finding a specific value in a sorted array. It works by comparing the middle value of the array and halving the search range. The time complexity of binary search is O(log n), making it very efficient. Let&#8217;s understand the principles of this algorithm by implementing it &hellip; \ub354 \ubcf4\uae30 \"C# Coding Test Course, Binary Search\"","og_url":"https:\/\/atmokpo.com\/w\/34068\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:23:46+00:00","article_modified_time":"2024-11-01T10:53:33+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":"1\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/34068\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34068\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"C# Coding Test Course, Binary Search","datePublished":"2024-11-01T09:23:46+00:00","dateModified":"2024-11-01T10:53:33+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34068\/"},"wordCount":414,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["C# Coding Test Tutorials"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34068\/","url":"https:\/\/atmokpo.com\/w\/34068\/","name":"C# Coding Test Course, Binary Search - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:23:46+00:00","dateModified":"2024-11-01T10:53:33+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34068\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34068\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34068\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"C# 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\/34068","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=34068"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34068\/revisions"}],"predecessor-version":[{"id":34069,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34068\/revisions\/34069"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34068"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34068"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34068"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}