{"id":6171,"date":"2025-05-29T18:45:59","date_gmt":"2025-05-29T13:15:59","guid":{"rendered":"https:\/\/namastedev.com\/blog\/?p=6171"},"modified":"2025-05-29T18:45:59","modified_gmt":"2025-05-29T13:15:59","slug":"linear-search","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/linear-search\/","title":{"rendered":"Linear Search"},"content":{"rendered":"\n<!-- Prism.js CSS and JS -->\n<link href=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/themes\/prism-tomorrow.css\" rel=\"stylesheet\" \/>\n<script src=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/prism.js\"><\/script>\n<script src=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/plugins\/autoloader\/prism-autoloader.min.js\"><\/script>\n\n<style>\n.wp_blog_code-tabs-container {\n    font-family: \"Segoe UI\", sans-serif !important;\n    max-width: 900px !important;\n    margin: 2rem auto !important;\n    border: 1px solid #ddd !important;\n    border-radius: 8px !important;\n    overflow: hidden !important;\n    background-color: white !important;\n}\n\n.wp_blog_code-tabs-header {\n    background: #f7f7f7 !important;\n    display: flex !important;\n    border-bottom: 1px solid #ddd !important;\n}\n\n.wp_blog_code-tab-button {\n    flex: 1 !important;\n    padding: 10px 15px !important;\n    border: none !important;\n    background: transparent !important;\n    cursor: pointer !important;\n    font-weight: bold !important;\n    transition: background 0.2s !important;\n    color: #242B33 !important;\n}\n\n.wp_blog_code-tab-button.active {\n    background: white !important;\n    border-bottom: 3px solid #0073aa !important;\n}\n\n.wp_blog_code-tab-content {\n    display: none !important;\n    padding: 20px !important;\n    background: #242B33 !important;\n}\n\n.wp_blog_code-tab-content > pre {\n    background: #242B33 !important;\n}\n\n.wp_blog_code-tab-content.active {\n    display: block !important;\n}\n\n.wp_blog_code-tab-content pre {\n    margin: 0 !important;\n    overflow-x: auto !important;\n}\n\n.wp_blog_explanation {\n    max-width: 900px !important;\n    margin: 2rem auto !important;\n    font-family: \"Segoe UI\", sans-serif !important;\n    line-height: 1.6 !important;\n    background: white !important;\n    color: black !important;\n    padding: 1rem !important;\n    border-radius: 8px !important;\n}\n\n.wp_blog_explanation h2 {\n    color: #0073aa !important;\n    font-size: 1.5rem !important;\n    margin-bottom: 0.5rem !important;\n}\n\n.wp_blog_explanation code {\n    background: #f1f1f1 !important;\n    padding: 2px 6px !important;\n    border-radius: 4px !important;\n    font-family: monospace !important;\n}\n\n.wp_blog_explanation h1,\n.wp_blog_explanation h2,\n.wp_blog_explanation h3,\n.wp_blog_explanation h4,\n.wp_blog_explanation h5,\n.wp_blog_explanation h6,\n.wp_blog_explanation p {\n    margin-top: 10px !important;\n    margin-bottom: 10px !important;\n}\n<\/style>\n\n<!-- \u2705 START: Blog Explanation -->\n<div class=\"wp_blog_explanation\">\n    <p>\n        <strong>Linear Search<\/strong> is a simple search algorithm used to find a specific element in an array. \n        It checks each element of the array one by one until the target value is found or the end of the array is reached.\n      <\/p>\n      <h2>Examples<\/h2>\n      <h3>Example 1:<\/h3>\n      <pre><code>Input: arr = [2, 4, 7, 10], target = 10\n    Output: 3\n    Explanation: 10 is found at index 3\n      <\/code><\/pre>\n    \n      <h3>Example 2:<\/h3>\n      <pre><code>Input: arr = [6, 8, 0, 3], target = 5\n    Output: -1\n    Explanation: 5 is not present in the array\n      <\/code><\/pre>\n      <h2>Approach:<\/h2>\n      <ul>\n        <li>Start from the first element of the array.<\/li>\n        <li>Compare the current element with the target value.<\/li>\n        <li>If a match is found, return the index.<\/li>\n        <li>If the loop ends without finding the target, return <code>-1<\/code>.<\/li>\n      <\/ul>\n    \n      <h2>Dry Run:<\/h2>\n      <div class=\"highlight\">\n        <strong>Input:<\/strong><br>\n        Array: [4, 5, 1, 3, 9] <br>\n        Target: 5\n      <\/div>\n      <ul>\n        <li>i = 0: arr[0] = 4 \u2192 Not equal to 5 \u2192 Continue<\/li>\n        <li>i = 1: arr[1] = 5 \u2192 Equal to 5 \u2192 <strong>Return 1<\/strong><\/li>\n      <\/ul>\n      <p><strong>Output:<\/strong> Element found at index <code>1<\/code><\/p>\n    \n      <h2>Time Complexity (TC):<\/h2>\n      <p>\n        &#8211; In the worst case, the algorithm traverses the entire array.<br>\n        &#8211; Each element is checked exactly once.<br>\n        <strong>TC = O(n)<\/strong>, where <code>n<\/code> is the size of the array.\n      <\/p>\n    \n      <h2>Space Complexity (SC):<\/h2>\n      <p>\n        &#8211; The algorithm does not use any extra space.<br>\n        <strong>SC = O(1)<\/strong> (constant space)\n      <\/p>\n<\/div>\n\n<div class=\"wp_blog_code-tabs-container\">\n    <div class=\"wp_blog_code-tabs-header\">\n      <button class=\"wp_blog_code-tab-button active\" data-lang=\"js\">JavaScript<\/button>\n      <button class=\"wp_blog_code-tab-button\" data-lang=\"cpp\">C++<\/button>\n      <button class=\"wp_blog_code-tab-button\" data-lang=\"c\">C<\/button>\n      <button class=\"wp_blog_code-tab-button\" data-lang=\"java\">Java<\/button>\n      <button class=\"wp_blog_code-tab-button\" data-lang=\"py\">Python<\/button>\n    <\/div>\n  \n    <div class=\"wp_blog_code-tab-content active\" data-lang=\"js\">\n      <pre><code class=\"language-javascript\">\n  let arr = [4, 5, 1, 3, 9];\n  \n  function linearSearch(arr, target) {\n    for (let i = 0; i < arr.length; i++) {\n      if (arr[i] == target) {\n        return i;\n      }\n    }\n    return -1;\n  }\n  \n  let result = linearSearch(arr, 5);\n  console.log(\"Element found at index\", result);\n      <\/code><\/pre>\n    <\/div>\n  \n    <div class=\"wp_blog_code-tab-content\" data-lang=\"cpp\">\n      <pre><code class=\"language-cpp\">\n  #include &lt;iostream&gt;\n  using namespace std;\n  \n  int linearSearch(int arr[], int size, int target) {\n    for (int i = 0; i < size; i++) {\n      if (arr[i] == target) {\n        return i;\n      }\n    }\n    return -1;\n  }\n  \n  int main() {\n    int arr[] = {4, 5, 1, 3, 9};\n    int result = linearSearch(arr, 5, 5);\n    cout &lt;&lt; \"Element found at index \" &lt;&lt; result &lt;&lt; endl;\n    return 0;\n  }\n      <\/code><\/pre>\n    <\/div>\n  \n    <div class=\"wp_blog_code-tab-content\" data-lang=\"c\">\n      <pre><code class=\"language-c\">\n  #include &lt;stdio.h&gt;\n  \n  int linearSearch(int arr[], int size, int target) {\n    for (int i = 0; i &lt; size; i++) {\n      if (arr[i] == target) {\n        return i;\n      }\n    }\n    return -1;\n  }\n  \n  int main() {\n    int arr[] = {4, 5, 1, 3, 9};\n    int size = sizeof(arr) \/ sizeof(arr[0]);\n    int result = linearSearch(arr, size, 5);\n    printf(\"Element found at index %d\\n\", result);\n    return 0;\n  }\n      <\/code><\/pre>\n    <\/div>\n  \n    <div class=\"wp_blog_code-tab-content\" data-lang=\"java\">\n      <pre><code class=\"language-java\">\n  public class Solution {\n    public static int linearSearch(int[] arr, int target) {\n      for (int i = 0; i &lt; arr.length; i++) {\n        if (arr[i] == target) {\n          return i;\n        }\n      }\n      return -1;\n    }\n  \n    public static void main(String[] args) {\n      int[] arr = {4, 5, 1, 3, 9};\n      int result = linearSearch(arr, 5);\n      System.out.println(\"Element found at index \" + result);\n    }\n  }\n      <\/code><\/pre>\n    <\/div>\n  \n    <div class=\"wp_blog_code-tab-content\" data-lang=\"py\">\n      <pre><code class=\"language-python\">\n  def linear_search(arr, target):\n      for i in range(len(arr)):\n          if arr[i] == target:\n              return i\n      return -1\n  \n  arr = [4, 5, 1, 3, 9]\n  result = linear_search(arr, 5)\n  print(\"Element found at index\", result)\n      <\/code><\/pre>\n    <\/div>\n  <\/div>\n  \n  \n<!-- \u2705 JS Tab Switch Logic -->\n<script>\ndocument.addEventListener('DOMContentLoaded', function () {\n    const buttons = document.querySelectorAll('.wp_blog_code-tab-button');\n    const contents = document.querySelectorAll('.wp_blog_code-tab-content');\n    buttons.forEach(button => {\n        button.addEventListener('click', () => {\n            const lang = button.getAttribute('data-lang');\n            buttons.forEach(btn => btn.classList.remove('active'));\n            button.classList.add('active');\n            contents.forEach(content => {\n                content.classList.toggle('active', content.getAttribute('data-lang') === lang);\n            });\n        });\n    });\n});\n<\/script>\n","protected":false},"excerpt":{"rendered":"<p>Linear Search is a simple search algorithm used to find a specific element in an array. It checks each element of the array one by one until the target value is found or the end of the array is reached. Examples Example 1: Input: arr = [2, 4, 7, 10], target = 10 Output: 3<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[811,810],"tags":[],"class_list":{"0":"post-6171","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-data-structures-and-algorithms","7":"category-dsa"},"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/6171","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/comments?post=6171"}],"version-history":[{"count":1,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/6171\/revisions"}],"predecessor-version":[{"id":6172,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/6171\/revisions\/6172"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=6171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=6171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=6171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}