{"id":11772,"date":"2023-01-17T09:49:52","date_gmt":"2023-01-17T09:49:52","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=11772"},"modified":"2023-04-25T09:18:31","modified_gmt":"2023-04-25T09:18:31","slug":"linear-search-in-data-structure","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/linear-search-in-data-structure\/","title":{"rendered":"Linear Search in Data Structure"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1673948531554-linear%20search%20in%20data%20structure.jpg\" alt=\"\" \/><\/p>\n<p>Linear search is a basic searching algorithm used in computer programming to find a specific element in a collection of data. It involves iterating through each element in the collection one by one until the target element is found. This algorithm is simple and easy to implement.<\/p>\n<p>One of the most widely applied programming techniques in daily life is searching. It&#8217;s everywhere, whether you&#8217;re browsing on Google, shopping on Amazon, or discovering the next Netflix series to binge. If you stop to think about it, you probably found this article by searching on Google or possibly Bing alone today.<\/p>\n<h2>Definition of Search in Computer Programming<\/h2>\n<p>In computer programming, the process of finding a particular item within a data structure is commonly known as search. This is often necessary when trying to extract useful information from a dataset.<\/p>\n<p><strong>For Example,<\/strong><br \/>\nLet\u2019s take the following example where we want to search for the word &quot;PrepBytes&quot; in the sentence:<\/p>\n<pre><code>\u201cLearn coding from PrepBytes\u201d<\/code><\/pre>\n<p>PrepBytes is the fourth word in the previous statement, as can be seen. When the data set is so little and you are using your naked sight to search, that is simple.<\/p>\n<p>It won\u2019t be so simple, though, if someone asks you to locate a word in a text that has tens of thousands of words and sentences.<\/p>\n<p>That\u2019s exactly what we\u2019ll study today: how to write a program to find an element in a data set.<\/p>\n<h2>Linear Search In Data Structure<\/h2>\n<p>Let&#8217;s imagine that we are attempting to find the value K within an array of random integers.<\/p>\n<p>Suppose the array is 5 3 12 9 45 1 22<br \/>\nK = 9 is the value of the element we&#8217;re looking for.<\/p>\n<p><strong>What is the most logical approach to take?<\/strong><br \/>\nYou&#8217;ve just defined Linear Search yourself if you&#8217;re planning to start at the beginning of the array and see if each element is equal to K or not until you either find K or reach the end.<\/p>\n<p>Let&#8217;s give it a clearer definition.<\/p>\n<p>In a linear search, we go over each element of the array one at a time and see if it matches the element we&#8217;re looking for. Because it examines each element in turn, it is also known as a sequential search.<\/p>\n<p>The linear search involves scanning an array or list sequentially until the desired element is found. If the value K is found during this process, it means that K is present in the array. Conversely, if K is not found after scanning through the entire array, it can be inferred that K is not in the array. Let\u2019s try using linear search for the input mentioned above.<\/p>\n<p>5 3 12 9 45 1 22 \u2013 Found K = 9 at index 3<\/p>\n<h2>Working of Linear Search In Data Structure Algorithm<\/h2>\n<p>As you can see, in the fourth comparison, we found K.<\/p>\n<p>You could counter that there is no need to begin at the beginning rather than the middle or the end. We can carry out either of those, you&#8217;re right. But consider this: Does it truly assist? No, really, is the response.<\/p>\n<p>Only the fourth iteration contains the number 9 if you attempt to search for it from the end rather than the beginning.<\/p>\n<p>5 3 12 9 45 1 22 \u2013 Found K = 9 at index 3<\/p>\n<h2>What is Linear Search In Data Structure Algorithm?<\/h2>\n<p>If K is present in the first half of the array, it will be simpler to start at the beginning if K is present. On the other hand, it is preferable to begin at the end if it appears in the second part.<\/p>\n<p>But stop and think about it\u2014do we really know where the element might be in a wholly random array? Or maybe just the fact that it exists or not? Right, we don&#8217;t. We use linear search for that purpose.<\/p>\n<p>In fact, it is very possible that K will be the first element of the array if you start at the end and the last if you start from the first.<\/p>\n<p>As a result, if we assume the worst, we will always end up searching the entire array. Therefore, it doesn&#8217;t really matter where we begin the search.<\/p>\n<h2>Algorithm For Linear Search In Data Structure<\/h2>\n<p>The algorithm for Linear Search in Data Structure is given below in a stepwise manner.<\/p>\n<ol>\n<li>Initialize i = 0 and n = size of array<\/li>\n<li>If i &gt;= n, which means we have reached the end of the array and we could not find K. We return -1 to signify that the element K was not found.<\/li>\n<li>If arr [ i ] == K, it means that we have found an element that is equal to K at index &#8216;i\u2019 and we do not need to search the remaining array.We immediately return the value i from here, which is the index in this array at which K was located.<\/li>\n<li>If arr [i]!= K, the current element of the array will not equal K, and we will repeat step 2 with the subsequent element by incrementing i i.e. ++i.<\/li>\n<\/ol>\n<h3>Dry Run For Linear Search In Data Structure<\/h3>\n<p>The dry run for linear search in data structure is given below for a better understanding of the topic.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1673948574639-linear%20search%20in%20data%20structure1.png\" alt=\"\" \/><\/p>\n<h3>Implementation of Linear Search in Data Structure<\/h3>\n<p>Below is a simple code implementation of Linear Search in Data Structure.<\/p>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_11724 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_11724 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_11724 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_11724 .wpsm_nav-tabs > li.active > a, #tab_container_11724 .wpsm_nav-tabs > li.active > a:hover, #tab_container_11724 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_11724 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_11724 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_11724 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_11724 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_11724 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_11724 .wpsm_nav-tabs > li > a:hover , #tab_container_11724 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_11724 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_11724 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_11724 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_11724 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_11724 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_11724 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_11724 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_11724 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_11724 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_11724 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_11724 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_11724\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_11724\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_11724_1\" aria-controls=\"tabs_desc_11724_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_11724_2\" aria-controls=\"tabs_desc_11724_2\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C++<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_11724_3\" aria-controls=\"tabs_desc_11724_3\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Java<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_11724_4\" aria-controls=\"tabs_desc_11724_4\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Python<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_11724\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_11724_1\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\">#include &lt;stdio.h&gt;\r\n\r\nint linearSearch(int arr[], int size, int key) {\r\n\/\/ If the size of the array is zero, return -1\r\nif (size == 0) {\r\n    return -1;\r\n}\r\n\r\n\/\/ Check if the element at the current index is equal to the key\r\nif (arr[size - 1] == key) {\r\n    \/\/ If equal, return the index\r\n    return size - 1;\r\n} else {\r\n    \/\/ If not equal, call the function again with the size reduced by 1\r\n    return linearSearch(arr, size - 1, key);\r\n}\r\n}\r\n\r\nint main() {\r\n\r\nint arr[] = {5, 2, 3, 4, 17, 21, 71, 10, 12, 14, 31};\r\nint key = 71;\r\nint index = linearSearch(arr, sizeof(arr) \/ sizeof(int), key);\r\nif (index == -1) {\r\n    printf(\"Key not found in the array.&#92;n\");\r\n} else {\r\n    printf(\"The element %d is found at %d index of the given array &#92;n\",key,index);\r\n}\r\nreturn 0;\r\n}\r\n<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_11724_2\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">#include &lt;iostream&gt;\r\nusing namespace std;\r\n\r\nint linearsearch(int arr[], int size, int key)\r\n{\r\n    if (size == 0) {\r\n        return -1;\r\n    }\r\n    else if (arr[size - 1] == key) {\r\n        \/\/ Return the index of found key.\r\n        return size - 1;\r\n    }\r\n    else {\r\n        int ans = linearsearch(arr, size - 1, key);\r\n        return ans;\r\n    }\r\n}\r\n\r\nint main()\r\n{\r\n    int arr[11] = {5, 2, 3, 4, 17, 21, 71, 10, 12, 14, 31};\r\n    int key = 71;\r\n\r\n    \/\/ Function call\r\n    int ans = linearsearch(arr, 11, key);\r\n    if (ans == -1) {\r\n        cout &lt;&lt; \"The element \" &lt;&lt; key &lt;&lt; \" is not found.\"\r\n            &lt;&lt; endl;\r\n    }\r\n    else {\r\n        cout &lt;&lt; \"The element \" &lt;&lt; key &lt;&lt; \" is found at \"\r\n            &lt;&lt; ans &lt;&lt; \" index of the given array.\" &lt;&lt; endl;\r\n    }\r\n    return 0;\r\n}\r\n<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_11724_3\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">import java.io.*;\r\n\r\nclass Test {\r\n    static int arr[] = {5, 2, 3, 4, 17, 21, 71, 10, 12, 14, 31};\r\n\r\n    \/\/ Recursive Method to search key in the array\r\n    static int linearsearch(int arr[], int size, int key)\r\n    {\r\n        if (size == 0) {\r\n            return -1;\r\n        }\r\n        else if (arr[size - 1] == key) {\r\n            \/\/ Return the index of found key.\r\n            return size - 1;\r\n        }\r\n        else {\r\n            return linearsearch(arr, size - 1, key);\r\n        }\r\n    }\r\n\r\n\r\n    public static void main(String[] args)\r\n    {\r\n        int key = 71;\r\n\r\n        \/\/ Function call to find key\r\n        int index = linearsearch(arr, arr.length, key);\r\n        if (index != -1)\r\n            System.out.println(\r\n                \"The element \" + key + \" is found at \"\r\n                + index + \" index of the given array.\");\r\n\r\n        else\r\n            System.out.println(\"The element \" + key\r\n                            + \" is not found.\");\r\n    }\r\n}\r\n<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_11724_4\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">def linear_search(arr, key, size):\r\n\r\n# If the array is empty we will return -1\r\n    if (size == 0):\r\n        return -1\r\n\r\n    elif (arr[size - 1] == key):\r\n        # Return the index of found key.\r\n        return size - 1\r\n    else:\r\n        return linear_search(arr, key, size - 1)\r\n\r\n\r\nif __name__ == \"__main__\":\r\n    arr = [5, 2, 3, 4, 17, 21, 71, 10, 12, 14, 31]\r\n    key = 71\r\n    size = len(arr)\r\n    ans = linear_search(arr, key, size) # Calling the Function\r\n    if ans != -1:\r\n        print(\"The element\", key, \"is found at\",\r\n            ans, \"index of the given array.\")\r\n    else:\r\n        print(\"The element\", key, \"is not found.\")\r\n<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_11724 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_11724 a\"),jQuery(\"#tab-content_11724\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<p><strong>Output<\/strong><\/p>\n<pre><code>The element 71 is found at 6 index of the given array.<\/code><\/pre>\n<h2>Time Complexity For Linear Search In Data Structure<\/h2>\n<p>The time complexity for the linear search in data structure is discussed below for different scenarios.<\/p>\n<p>In the best-case scenario, K, which is the first element of the array, might only be found in the first iteration.<\/p>\n<p>In the worst scenario, K might not even be present in the array by the time we reach the final iteration.<\/p>\n<p>It might typically be found anywhere in the middle of the array.<\/p>\n<p>The average case time complexity of the linear search is therefore probably O(n\/2) ~ O(n), where n is the number of elements in the array. Consequently, the linear search&#8217;s time complexity is, in fact, linear.<\/p>\n<h2>Space Complexity For Linear Search In Data Structure<\/h2>\n<p>It is clear that we do not significantly increase our memory usage for linear search. As a result, the space complexity is constant, or O(1).<\/p>\n<p><strong>Note:<\/strong> Since they are independent of the input size of the array, the variables used to iterate the array and other minor information are constant.<\/p>\n<h2>Advantages of Linear Search In Data Structure<\/h2>\n<p>Linear search has several advantages which are listed below.<\/p>\n<ul>\n<li>It is a simple algorithm to implement and understand. <\/li>\n<li>It can be applied regardless of whether the array is sorted or not and is also compatible with arrays of different data types. <\/li>\n<li>Additionally, it does not require any extra memory to execute, making it efficient in terms of space usage. <\/li>\n<li>Linear search is particularly suitable for small datasets due to its straightforward nature.<\/li>\n<\/ul>\n<h2>Disadvantages of Linear Search In Data Structure<\/h2>\n<p>Here are some disadvantages of linear search in data structure.<\/p>\n<ul>\n<li>Its time complexity of O(n) can cause it to be slow when dealing with large datasets. Therefore, it may not be suitable for searching large arrays. <\/li>\n<li>Additionally, other search algorithms like hash tables may offer better efficiency in terms of time complexity.<\/li>\n<\/ul>\n<h2>Applications For Linear Search In Data Structure<\/h2>\n<p>The most straightforward and simple to use and understand algorithm is linear search. Since it always consumes constant memory, it is especially helpful when the data set is tiny, randomized, and there are memory restrictions.<\/p>\n<p>Since linear search will take linear time in each case on average, it is not the most effective strategy when searching several elements in a data collection.<\/p>\n<p><strong>Conclusion<\/strong><br \/>\nThe process of searching is pretty fascinating, especially to me because there are so many uses for it and so many obstacles to overcome in order to search well. It&#8217;s also one of the most frequent interview topics. So put on your hunting boots and start looking.<\/p>\n<h2>Frequently Asked Questions(FAQs)<\/h2>\n<p>Here are some frequently asked questions on linear search in data structure.<\/p>\n<p><strong>Ques 1. When is linear search most useful?<\/strong><br \/>\n<strong>Ans.<\/strong> Linear search is most useful for small datasets where efficiency is not a primary concern and simplicity is preferred.<\/p>\n<p><strong>Ques 2. Can linear search be used for strings?<\/strong><br \/>\n<strong>Ans.<\/strong> Yes, the linear search can be used for any data type, including strings.<\/p>\n<p><strong>Ques 3. How is linear search different from binary search?<\/strong><br \/>\n<strong>Ans.<\/strong> Linear search in data structure iterates through each element in a collection sequentially, while binary search requires the collection to be sorted and search by dividing the collection in half and eliminating one-half of each iteration.<\/p>\n<p><strong>Ques 4. Is linear search the most efficient search algorithm?<\/strong><br \/>\n<strong>Ans.<\/strong> No, linear search in data structure has a time complexity of O(n), meaning it can be inefficient for large datasets. Other search algorithms like binary search and hash tables may be more efficient.<\/p>\n<p><strong>Ques 5. Can linear search be used for linked lists?<\/strong><br \/>\n<strong>Ans.<\/strong> Yes, linear search in data structure can be used for linked lists by iterating through each node in the list until the target element is found.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Linear search is a basic searching algorithm used in computer programming to find a specific element in a collection of data. It involves iterating through each element in the collection one by one until the target element is found. This algorithm is simple and easy to implement. One of the most widely applied programming techniques [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[191],"tags":[],"class_list":["post-11772","post","type-post","status-publish","format-standard","hentry","category-data-structure"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Linear Search In Data Structure<\/title>\n<meta name=\"description\" content=\"In linear search, each item is sequentially searched through. Also understand working and Space and Time Complexity of linear search.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prepbytes.com\/blog\/linear-search-in-data-structure\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Linear Search In Data Structure\" \/>\n<meta property=\"og:description\" content=\"In linear search, each item is sequentially searched through. Also understand working and Space and Time Complexity of linear search.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/linear-search-in-data-structure\/\" \/>\n<meta property=\"og:site_name\" content=\"PrepBytes Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prepbytes0211\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-01-17T09:49:52+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-04-25T09:18:31+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1673948531554-linear%20search%20in%20data%20structure.jpg\" \/>\n<meta name=\"author\" content=\"Prepbytes\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prepbytes\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/linear-search-in-data-structure\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/linear-search-in-data-structure\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"Linear Search in Data Structure\",\"datePublished\":\"2023-01-17T09:49:52+00:00\",\"dateModified\":\"2023-04-25T09:18:31+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/linear-search-in-data-structure\/\"},\"wordCount\":1500,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/linear-search-in-data-structure\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1673948531554-linear%20search%20in%20data%20structure.jpg\",\"articleSection\":[\"Data Structure\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/linear-search-in-data-structure\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/linear-search-in-data-structure\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/linear-search-in-data-structure\/\",\"name\":\"Linear Search In Data Structure\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/linear-search-in-data-structure\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/linear-search-in-data-structure\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1673948531554-linear%20search%20in%20data%20structure.jpg\",\"datePublished\":\"2023-01-17T09:49:52+00:00\",\"dateModified\":\"2023-04-25T09:18:31+00:00\",\"description\":\"In linear search, each item is sequentially searched through. Also understand working and Space and Time Complexity of linear search.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/linear-search-in-data-structure\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/linear-search-in-data-structure\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/linear-search-in-data-structure\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1673948531554-linear%20search%20in%20data%20structure.jpg\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1673948531554-linear%20search%20in%20data%20structure.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/linear-search-in-data-structure\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/43.205.93.38\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Data Structure\",\"item\":\"https:\/\/prepbytes.com\/blog\/category\/data-structure\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Linear Search in Data Structure\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/43.205.93.38\/#website\",\"url\":\"http:\/\/43.205.93.38\/\",\"name\":\"PrepBytes Blog\",\"description\":\"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING\",\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/43.205.93.38\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/43.205.93.38\/#organization\",\"name\":\"Prepbytes\",\"url\":\"http:\/\/43.205.93.38\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"contentUrl\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"width\":160,\"height\":160,\"caption\":\"Prepbytes\"},\"image\":{\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/prepbytes0211\/\",\"https:\/\/www.instagram.com\/prepbytes\/\",\"https:\/\/www.linkedin.com\/company\/prepbytes\/\",\"https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\",\"name\":\"Prepbytes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"caption\":\"Prepbytes\"},\"url\":\"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Linear Search In Data Structure","description":"In linear search, each item is sequentially searched through. Also understand working and Space and Time Complexity of linear search.","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:\/\/prepbytes.com\/blog\/linear-search-in-data-structure\/","og_locale":"en_US","og_type":"article","og_title":"Linear Search In Data Structure","og_description":"In linear search, each item is sequentially searched through. Also understand working and Space and Time Complexity of linear search.","og_url":"https:\/\/prepbytes.com\/blog\/linear-search-in-data-structure\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2023-01-17T09:49:52+00:00","article_modified_time":"2023-04-25T09:18:31+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1673948531554-linear%20search%20in%20data%20structure.jpg","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/linear-search-in-data-structure\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/linear-search-in-data-structure\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"Linear Search in Data Structure","datePublished":"2023-01-17T09:49:52+00:00","dateModified":"2023-04-25T09:18:31+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/linear-search-in-data-structure\/"},"wordCount":1500,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/linear-search-in-data-structure\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1673948531554-linear%20search%20in%20data%20structure.jpg","articleSection":["Data Structure"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/linear-search-in-data-structure\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/linear-search-in-data-structure\/","url":"https:\/\/prepbytes.com\/blog\/linear-search-in-data-structure\/","name":"Linear Search In Data Structure","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/linear-search-in-data-structure\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/linear-search-in-data-structure\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1673948531554-linear%20search%20in%20data%20structure.jpg","datePublished":"2023-01-17T09:49:52+00:00","dateModified":"2023-04-25T09:18:31+00:00","description":"In linear search, each item is sequentially searched through. Also understand working and Space and Time Complexity of linear search.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/linear-search-in-data-structure\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/linear-search-in-data-structure\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/linear-search-in-data-structure\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1673948531554-linear%20search%20in%20data%20structure.jpg","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1673948531554-linear%20search%20in%20data%20structure.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/linear-search-in-data-structure\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/43.205.93.38\/"},{"@type":"ListItem","position":2,"name":"Data Structure","item":"https:\/\/prepbytes.com\/blog\/category\/data-structure\/"},{"@type":"ListItem","position":3,"name":"Linear Search in Data Structure"}]},{"@type":"WebSite","@id":"http:\/\/43.205.93.38\/#website","url":"http:\/\/43.205.93.38\/","name":"PrepBytes Blog","description":"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING","publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/43.205.93.38\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/43.205.93.38\/#organization","name":"Prepbytes","url":"http:\/\/43.205.93.38\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/","url":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","contentUrl":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","width":160,"height":160,"caption":"Prepbytes"},"image":{"@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/prepbytes0211\/","https:\/\/www.instagram.com\/prepbytes\/","https:\/\/www.linkedin.com\/company\/prepbytes\/","https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA"]},{"@type":"Person","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e","name":"Prepbytes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","caption":"Prepbytes"},"url":"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/"}]}},"_links":{"self":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/11772","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/comments?post=11772"}],"version-history":[{"count":4,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/11772\/revisions"}],"predecessor-version":[{"id":15976,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/11772\/revisions\/15976"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=11772"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=11772"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=11772"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}