{"id":2212,"date":"2024-11-15T08:16:18","date_gmt":"2024-11-15T08:16:18","guid":{"rendered":"https:\/\/www.w3computing.com\/articles\/?p=2212"},"modified":"2024-11-15T08:16:21","modified_gmt":"2024-11-15T08:16:21","slug":"how-to-implement-a-basic-search-algorithm-in-java","status":"publish","type":"post","link":"https:\/\/www.w3computing.com\/articles\/how-to-implement-a-basic-search-algorithm-in-java\/","title":{"rendered":"How to Implement a Basic Search Algorithm in Java"},"content":{"rendered":"\n<p>Search algorithms are an integral part of programming and computer science. They are used to retrieve information stored within a data structure or database, and their efficiency directly impacts the performance of applications. In this tutorial, we will take a step-by-step approach to implementing basic search algorithms in Java.<\/p>\n\n\n\n<p>We will cover the implementation of linear search and binary search. These are foundational algorithms and are perfect for understanding how search works at a basic level. Along the way, I will explain each step thoroughly, and by the end, you will have a clear understanding of these algorithms and how to implement them in Java.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Prerequisites<\/h3>\n\n\n\n<p>Before starting, ensure you have the following:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Java Development Kit (JDK)<\/strong> installed on your system.<\/li>\n\n\n\n<li>An Integrated Development Environment (IDE) like IntelliJ IDEA, Eclipse, or Visual Studio Code (or just a simple text editor with a terminal).<\/li>\n\n\n\n<li>Basic understanding of Java syntax, loops, and arrays.<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Understanding the Search Problem<\/h3>\n\n\n\n<p>A search algorithm is designed to find the location of a specific element in a collection. The collection can be an array, list, or any other data structure. To keep things simple, we\u2019ll use arrays.<\/p>\n\n\n\n<p>There are two major categories of search algorithms:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Linear Search<\/strong>: Checks each element in sequence.<\/li>\n\n\n\n<li><strong>Binary Search<\/strong>: Works on sorted collections and divides the search space in half with each step.<\/li>\n<\/ol>\n\n\n\n<p>Let\u2019s dive into their implementation.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Implementing Linear Search in Java<\/h3>\n\n\n\n<p>Linear search is the simplest searching algorithm. It checks each element in the array one by one until the desired element is found or the array ends.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Algorithm<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Start at the first element of the array.<\/li>\n\n\n\n<li>Compare the target element with the current element.<\/li>\n\n\n\n<li>If they match, return the index.<\/li>\n\n\n\n<li>If they don\u2019t match, move to the next element.<\/li>\n\n\n\n<li>Repeat until the element is found or the array ends.<\/li>\n<\/ol>\n\n\n\n<p>Let\u2019s implement linear search in Java.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">LinearSearch<\/span> <\/span>{\n\n    <span class=\"hljs-comment\">\/\/ Linear Search Function<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">int<\/span> <span class=\"hljs-title\">linearSearch<\/span><span class=\"hljs-params\">(<span class=\"hljs-keyword\">int<\/span>&#91;] array, <span class=\"hljs-keyword\">int<\/span> target)<\/span> <\/span>{\n        <span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-keyword\">int<\/span> i = <span class=\"hljs-number\">0<\/span>; i &lt; array.length; i++) {\n            <span class=\"hljs-keyword\">if<\/span> (array&#91;i] == target) {\n                <span class=\"hljs-keyword\">return<\/span> i; <span class=\"hljs-comment\">\/\/ Return the index of the target element<\/span>\n            }\n        }\n        <span class=\"hljs-keyword\">return<\/span> -<span class=\"hljs-number\">1<\/span>; <span class=\"hljs-comment\">\/\/ Element not found<\/span>\n    }\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">(String&#91;] args)<\/span> <\/span>{\n        <span class=\"hljs-keyword\">int<\/span>&#91;] numbers = {<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">20<\/span>, <span class=\"hljs-number\">30<\/span>, <span class=\"hljs-number\">40<\/span>, <span class=\"hljs-number\">50<\/span>};\n        <span class=\"hljs-keyword\">int<\/span> target = <span class=\"hljs-number\">30<\/span>;\n\n        <span class=\"hljs-keyword\">int<\/span> result = linearSearch(numbers, target);\n\n        <span class=\"hljs-keyword\">if<\/span> (result != -<span class=\"hljs-number\">1<\/span>) {\n            System.out.println(<span class=\"hljs-string\">\"Element found at index: \"<\/span> + result);\n        } <span class=\"hljs-keyword\">else<\/span> {\n            System.out.println(<span class=\"hljs-string\">\"Element not found in the array.\"<\/span>);\n        }\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h4 class=\"wp-block-heading\">Explanation<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Input Array<\/strong>: <code>{10, 20, 30, 40, 50}<\/code><\/li>\n\n\n\n<li><strong>Target Element<\/strong>: <code>30<\/code><\/li>\n\n\n\n<li>The function <code>linearSearch<\/code> loops through the array.<\/li>\n\n\n\n<li>It checks each element and compares it with <code>30<\/code>.<\/li>\n\n\n\n<li>If found, it returns the index. Otherwise, it returns <code>-1<\/code>.<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">Output<\/h4>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">Element found at index: 2<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Analyzing Linear Search<\/h3>\n\n\n\n<p>Linear search has a straightforward implementation, but its performance is suboptimal for large datasets.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Best Case<\/strong>: The element is at the first position (<code>O(1)<\/code>).<\/li>\n\n\n\n<li><strong>Worst Case<\/strong>: The element is not in the array, or it is at the last position (<code>O(n)<\/code>).<\/li>\n\n\n\n<li><strong>Average Case<\/strong>: <code>O(n)<\/code> where <code>n<\/code> is the size of the array.<\/li>\n<\/ul>\n\n\n\n<p>Linear search is useful for small datasets or unsorted data, but it becomes inefficient as the dataset grows.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Understanding Binary Search<\/h3>\n\n\n\n<p>Binary search is a much more efficient algorithm but requires the array to be sorted. It works by dividing the search space into two halves, eliminating one half in each step.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Algorithm<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Find the middle element of the array.<\/li>\n\n\n\n<li>If the middle element is the target, return its index.<\/li>\n\n\n\n<li>If the target is smaller than the middle element, search in the left half.<\/li>\n\n\n\n<li>If the target is larger, search in the right half.<\/li>\n\n\n\n<li>Repeat until the target is found or the subarray becomes empty.<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Step 5: Implementing Binary Search in Java<\/h3>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">BinarySearch<\/span> <\/span>{\n\n    <span class=\"hljs-comment\">\/\/ Binary Search Function<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">int<\/span> <span class=\"hljs-title\">binarySearch<\/span><span class=\"hljs-params\">(<span class=\"hljs-keyword\">int<\/span>&#91;] array, <span class=\"hljs-keyword\">int<\/span> target)<\/span> <\/span>{\n        <span class=\"hljs-keyword\">int<\/span> left = <span class=\"hljs-number\">0<\/span>;\n        <span class=\"hljs-keyword\">int<\/span> right = array.length - <span class=\"hljs-number\">1<\/span>;\n\n        <span class=\"hljs-keyword\">while<\/span> (left &lt;= right) {\n            <span class=\"hljs-keyword\">int<\/span> mid = left + (right - left) \/ <span class=\"hljs-number\">2<\/span>;\n\n            <span class=\"hljs-comment\">\/\/ Check if the target is at mid<\/span>\n            <span class=\"hljs-keyword\">if<\/span> (array&#91;mid] == target) {\n                <span class=\"hljs-keyword\">return<\/span> mid;\n            }\n\n            <span class=\"hljs-comment\">\/\/ If target is smaller, ignore right half<\/span>\n            <span class=\"hljs-keyword\">if<\/span> (target &lt; array&#91;mid]) {\n                right = mid - <span class=\"hljs-number\">1<\/span>;\n            }\n            <span class=\"hljs-comment\">\/\/ If target is larger, ignore left half<\/span>\n            <span class=\"hljs-keyword\">else<\/span> {\n                left = mid + <span class=\"hljs-number\">1<\/span>;\n            }\n        }\n\n        <span class=\"hljs-keyword\">return<\/span> -<span class=\"hljs-number\">1<\/span>; <span class=\"hljs-comment\">\/\/ Element not found<\/span>\n    }\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">(String&#91;] args)<\/span> <\/span>{\n        <span class=\"hljs-keyword\">int<\/span>&#91;] numbers = {<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">20<\/span>, <span class=\"hljs-number\">30<\/span>, <span class=\"hljs-number\">40<\/span>, <span class=\"hljs-number\">50<\/span>}; <span class=\"hljs-comment\">\/\/ Must be sorted<\/span>\n        <span class=\"hljs-keyword\">int<\/span> target = <span class=\"hljs-number\">40<\/span>;\n\n        <span class=\"hljs-keyword\">int<\/span> result = binarySearch(numbers, target);\n\n        <span class=\"hljs-keyword\">if<\/span> (result != -<span class=\"hljs-number\">1<\/span>) {\n            System.out.println(<span class=\"hljs-string\">\"Element found at index: \"<\/span> + result);\n        } <span class=\"hljs-keyword\">else<\/span> {\n            System.out.println(<span class=\"hljs-string\">\"Element not found in the array.\"<\/span>);\n        }\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h4 class=\"wp-block-heading\">Explanation<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>The <code>binarySearch<\/code> function initializes <code>left<\/code> and <code>right<\/code> pointers.<\/li>\n\n\n\n<li>It calculates the <code>mid<\/code> index and compares the target with <code>array[mid]<\/code>.<\/li>\n\n\n\n<li>Based on the comparison, it adjusts the search bounds (<code>left<\/code> or <code>right<\/code>).<\/li>\n\n\n\n<li>If the target is found, it returns the index. Otherwise, it returns <code>-1<\/code>.<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">Output<\/h4>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">Element found at index: 3<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Step 6: Binary Search with Recursion<\/h3>\n\n\n\n<p>Binary search can also be implemented using recursion. Here\u2019s how:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Java\" data-shcb-language-slug=\"java\"><span><code class=\"hljs language-java\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">RecursiveBinarySearch<\/span> <\/span>{\n\n    <span class=\"hljs-comment\">\/\/ Recursive Binary Search Function<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">int<\/span> <span class=\"hljs-title\">binarySearchRecursive<\/span><span class=\"hljs-params\">(<span class=\"hljs-keyword\">int<\/span>&#91;] array, <span class=\"hljs-keyword\">int<\/span> target, <span class=\"hljs-keyword\">int<\/span> left, <span class=\"hljs-keyword\">int<\/span> right)<\/span> <\/span>{\n        <span class=\"hljs-keyword\">if<\/span> (left &gt; right) {\n            <span class=\"hljs-keyword\">return<\/span> -<span class=\"hljs-number\">1<\/span>; <span class=\"hljs-comment\">\/\/ Element not found<\/span>\n        }\n\n        <span class=\"hljs-keyword\">int<\/span> mid = left + (right - left) \/ <span class=\"hljs-number\">2<\/span>;\n\n        <span class=\"hljs-comment\">\/\/ Check if the target is at mid<\/span>\n        <span class=\"hljs-keyword\">if<\/span> (array&#91;mid] == target) {\n            <span class=\"hljs-keyword\">return<\/span> mid;\n        }\n\n        <span class=\"hljs-comment\">\/\/ If target is smaller, search in the left half<\/span>\n        <span class=\"hljs-keyword\">if<\/span> (target &lt; array&#91;mid]) {\n            <span class=\"hljs-keyword\">return<\/span> binarySearchRecursive(array, target, left, mid - <span class=\"hljs-number\">1<\/span>);\n        }\n\n        <span class=\"hljs-comment\">\/\/ If target is larger, search in the right half<\/span>\n        <span class=\"hljs-keyword\">return<\/span> binarySearchRecursive(array, target, mid + <span class=\"hljs-number\">1<\/span>, right);\n    }\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">(String&#91;] args)<\/span> <\/span>{\n        <span class=\"hljs-keyword\">int<\/span>&#91;] numbers = {<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">20<\/span>, <span class=\"hljs-number\">30<\/span>, <span class=\"hljs-number\">40<\/span>, <span class=\"hljs-number\">50<\/span>}; <span class=\"hljs-comment\">\/\/ Must be sorted<\/span>\n        <span class=\"hljs-keyword\">int<\/span> target = <span class=\"hljs-number\">50<\/span>;\n\n        <span class=\"hljs-keyword\">int<\/span> result = binarySearchRecursive(numbers, target, <span class=\"hljs-number\">0<\/span>, numbers.length - <span class=\"hljs-number\">1<\/span>);\n\n        <span class=\"hljs-keyword\">if<\/span> (result != -<span class=\"hljs-number\">1<\/span>) {\n            System.out.println(<span class=\"hljs-string\">\"Element found at index: \"<\/span> + result);\n        } <span class=\"hljs-keyword\">else<\/span> {\n            System.out.println(<span class=\"hljs-string\">\"Element not found in the array.\"<\/span>);\n        }\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Java<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">java<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Step 7: Analyzing Binary Search<\/h3>\n\n\n\n<p>Binary search significantly reduces the number of comparisons:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Best Case<\/strong>: The element is at the middle (<code>O(1)<\/code>).<\/li>\n\n\n\n<li><strong>Worst Case<\/strong>: <code>O(log n)<\/code> where <code>n<\/code> is the size of the array.<\/li>\n\n\n\n<li><strong>Space Complexity<\/strong>: <code>O(1)<\/code> for iterative and <code>O(log n)<\/code> for recursive (due to call stack).<\/li>\n<\/ul>\n\n\n\n<p>Binary search is efficient for large sorted datasets.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 8: Comparing Linear Search and Binary Search<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th><strong>Feature<\/strong><\/th><th><strong>Linear Search<\/strong><\/th><th><strong>Binary Search<\/strong><\/th><\/tr><\/thead><tbody><tr><td><strong>Complexity<\/strong><\/td><td>O(n)<\/td><td>O(log n)<\/td><\/tr><tr><td><strong>Data Requirement<\/strong><\/td><td>Unsorted or sorted data<\/td><td>Requires sorted data<\/td><\/tr><tr><td><strong>Use Case<\/strong><\/td><td>Small datasets<\/td><td>Large sorted datasets<\/td><\/tr><tr><td><strong>Implementation<\/strong><\/td><td>Simple<\/td><td>More complex<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 9: Testing the Algorithms<\/strong><\/h3>\n\n\n\n<p>It\u2019s crucial to test both algorithms with different inputs. Below are a few examples:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Test Case 1<\/h4>\n\n\n\n<p>Input Array: <code>{5, 15, 25, 35, 45}<\/code><br>Target: <code>25<\/code><\/p>\n\n\n\n<p>Expected Output:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Linear Search: <code>Element found at index: 2<\/code><\/li>\n\n\n\n<li>Binary Search: <code>Element found at index: 2<\/code><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">Test Case 2<\/h4>\n\n\n\n<p>Input Array: <code>{3, 6, 9, 12, 15}<\/code><br>Target: <code>10<\/code><\/p>\n\n\n\n<p>Expected Output:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Linear Search: <code>Element not found in the array.<\/code><\/li>\n\n\n\n<li>Binary Search: <code>Element not found in the array.<\/code><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Search algorithms are an integral part of programming and computer science. They are used to retrieve information stored within a data structure or database, and their efficiency directly impacts the performance of applications. In this tutorial, we will take a step-by-step approach to implementing basic search algorithms in Java. We will cover the implementation of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[5,4],"tags":[],"class_list":{"0":"post-2212","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-java","7":"category-programming-languages","8":"entry"},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Implement a Basic Search Algorithm in Java<\/title>\n<meta name=\"description\" content=\"Search algorithms are an integral part of programming and computer science. They are used to retrieve information stored within a data structure\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.w3computing.com\/articles\/how-to-implement-a-basic-search-algorithm-in-java\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Implement a Basic Search Algorithm in Java\" \/>\n<meta property=\"og:description\" content=\"Search algorithms are an integral part of programming and computer science. They are used to retrieve information stored within a data structure\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.w3computing.com\/articles\/how-to-implement-a-basic-search-algorithm-in-java\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-15T08:16:18+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-15T08:16:21+00:00\" \/>\n<meta name=\"author\" content=\"w3compadmin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"w3compadmin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"TechArticle\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/how-to-implement-a-basic-search-algorithm-in-java\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/how-to-implement-a-basic-search-algorithm-in-java\\\/\"},\"author\":{\"name\":\"w3compadmin\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#\\\/schema\\\/person\\\/a550b3e20d78bb4f79b7c6b7b53f0561\"},\"headline\":\"How to Implement a Basic Search Algorithm in Java\",\"datePublished\":\"2024-11-15T08:16:18+00:00\",\"dateModified\":\"2024-11-15T08:16:21+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/how-to-implement-a-basic-search-algorithm-in-java\\\/\"},\"wordCount\":695,\"articleSection\":[\"Java\",\"Programming Languages\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/how-to-implement-a-basic-search-algorithm-in-java\\\/\",\"url\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/how-to-implement-a-basic-search-algorithm-in-java\\\/\",\"name\":\"How to Implement a Basic Search Algorithm in Java\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#website\"},\"datePublished\":\"2024-11-15T08:16:18+00:00\",\"dateModified\":\"2024-11-15T08:16:21+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#\\\/schema\\\/person\\\/a550b3e20d78bb4f79b7c6b7b53f0561\"},\"description\":\"Search algorithms are an integral part of programming and computer science. They are used to retrieve information stored within a data structure\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/how-to-implement-a-basic-search-algorithm-in-java\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/how-to-implement-a-basic-search-algorithm-in-java\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/how-to-implement-a-basic-search-algorithm-in-java\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Articles Home\",\"item\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Programming Languages\",\"item\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/programming-languages\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"How to Implement a Basic Search Algorithm in Java\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#website\",\"url\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/\",\"name\":\"Developer Articles Hub\",\"description\":\"\",\"alternateName\":\"Developer Articles\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#\\\/schema\\\/person\\\/a550b3e20d78bb4f79b7c6b7b53f0561\",\"name\":\"w3compadmin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/wp-content\\\/litespeed\\\/avatar\\\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1776115684\",\"url\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/wp-content\\\/litespeed\\\/avatar\\\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1776115684\",\"contentUrl\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/wp-content\\\/litespeed\\\/avatar\\\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1776115684\",\"caption\":\"w3compadmin\"},\"sameAs\":[\"http:\\\/\\\/w3computing.com\\\/articles\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Implement a Basic Search Algorithm in Java","description":"Search algorithms are an integral part of programming and computer science. They are used to retrieve information stored within a data structure","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:\/\/www.w3computing.com\/articles\/how-to-implement-a-basic-search-algorithm-in-java\/","og_locale":"en_US","og_type":"article","og_title":"How to Implement a Basic Search Algorithm in Java","og_description":"Search algorithms are an integral part of programming and computer science. They are used to retrieve information stored within a data structure","og_url":"https:\/\/www.w3computing.com\/articles\/how-to-implement-a-basic-search-algorithm-in-java\/","article_published_time":"2024-11-15T08:16:18+00:00","article_modified_time":"2024-11-15T08:16:21+00:00","author":"w3compadmin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"w3compadmin","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"TechArticle","@id":"https:\/\/www.w3computing.com\/articles\/how-to-implement-a-basic-search-algorithm-in-java\/#article","isPartOf":{"@id":"https:\/\/www.w3computing.com\/articles\/how-to-implement-a-basic-search-algorithm-in-java\/"},"author":{"name":"w3compadmin","@id":"https:\/\/www.w3computing.com\/articles\/#\/schema\/person\/a550b3e20d78bb4f79b7c6b7b53f0561"},"headline":"How to Implement a Basic Search Algorithm in Java","datePublished":"2024-11-15T08:16:18+00:00","dateModified":"2024-11-15T08:16:21+00:00","mainEntityOfPage":{"@id":"https:\/\/www.w3computing.com\/articles\/how-to-implement-a-basic-search-algorithm-in-java\/"},"wordCount":695,"articleSection":["Java","Programming Languages"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.w3computing.com\/articles\/how-to-implement-a-basic-search-algorithm-in-java\/","url":"https:\/\/www.w3computing.com\/articles\/how-to-implement-a-basic-search-algorithm-in-java\/","name":"How to Implement a Basic Search Algorithm in Java","isPartOf":{"@id":"https:\/\/www.w3computing.com\/articles\/#website"},"datePublished":"2024-11-15T08:16:18+00:00","dateModified":"2024-11-15T08:16:21+00:00","author":{"@id":"https:\/\/www.w3computing.com\/articles\/#\/schema\/person\/a550b3e20d78bb4f79b7c6b7b53f0561"},"description":"Search algorithms are an integral part of programming and computer science. They are used to retrieve information stored within a data structure","breadcrumb":{"@id":"https:\/\/www.w3computing.com\/articles\/how-to-implement-a-basic-search-algorithm-in-java\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.w3computing.com\/articles\/how-to-implement-a-basic-search-algorithm-in-java\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.w3computing.com\/articles\/how-to-implement-a-basic-search-algorithm-in-java\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Articles Home","item":"https:\/\/www.w3computing.com\/articles\/"},{"@type":"ListItem","position":2,"name":"Programming Languages","item":"https:\/\/www.w3computing.com\/articles\/programming-languages\/"},{"@type":"ListItem","position":3,"name":"How to Implement a Basic Search Algorithm in Java"}]},{"@type":"WebSite","@id":"https:\/\/www.w3computing.com\/articles\/#website","url":"https:\/\/www.w3computing.com\/articles\/","name":"Developer Articles Hub","description":"","alternateName":"Developer Articles","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.w3computing.com\/articles\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.w3computing.com\/articles\/#\/schema\/person\/a550b3e20d78bb4f79b7c6b7b53f0561","name":"w3compadmin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.w3computing.com\/articles\/wp-content\/litespeed\/avatar\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1776115684","url":"https:\/\/www.w3computing.com\/articles\/wp-content\/litespeed\/avatar\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1776115684","contentUrl":"https:\/\/www.w3computing.com\/articles\/wp-content\/litespeed\/avatar\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1776115684","caption":"w3compadmin"},"sameAs":["http:\/\/w3computing.com\/articles"]}]}},"featured_image_src":null,"featured_image_src_square":null,"author_info":{"display_name":"w3compadmin","author_link":"https:\/\/www.w3computing.com\/articles\/author\/w3compadmin\/"},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/posts\/2212","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/comments?post=2212"}],"version-history":[{"count":1,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/posts\/2212\/revisions"}],"predecessor-version":[{"id":2213,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/posts\/2212\/revisions\/2213"}],"wp:attachment":[{"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/media?parent=2212"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/categories?post=2212"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/tags?post=2212"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}