{"id":1327,"date":"2016-06-09T08:11:22","date_gmt":"2016-06-09T08:11:22","guid":{"rendered":"http:\/\/www.testingdocs.com\/questions\/?p=1327"},"modified":"2021-09-20T13:46:45","modified_gmt":"2021-09-20T13:46:45","slug":"write-a-java-program-for-linear-search-an-unsorted-array","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/write-a-java-program-for-linear-search-an-unsorted-array\/","title":{"rendered":"Java program for linear search an unsorted array?"},"content":{"rendered":"<h3>Problem statement<\/h3>\n<p>Write a java program for linear search of an unsorted array. Run your program with a few testcases do demonstrate your program.<\/p>\n<p>An unsorted or unordered array is an array where elements of the array are not sorted. To search for an element in this array, you have to scan the entire array to check if the element is present or not.<\/p>\n<h3><strong>Flow Chart for linear search<\/strong><\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1244\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Linear-Search-Flow-Chart.png\" alt=\"Linear Search Flow Chart\" width=\"820\" height=\"620\" title=\"\"><\/p>\n<p>&nbsp;<\/p>\n<h3>Environment<\/h3>\n<p>Tools used to develop the program:<\/p>\n<ul>\n<li>JDK<\/li>\n<li>IntelliJ IDE<\/li>\n<\/ul>\n<h3><strong>Java Program<\/strong><\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import java.util.InputMismatchException;\r\nimport java.util.Scanner;\r\n\r\npublic class LinearSearch {\r\n\r\n    public static void main(String[] args) {\r\n        int n , index = -1;\r\n        Scanner input = null;\r\n        int[] a = null;\r\n        try {\r\n            input=new Scanner(System.in);\r\n            System.out.println(\"Enter size of the array:\");\r\n            n= input.nextInt();\r\n\r\n            if(n &gt; 1) {\r\n                a=new int[n];\r\n                System.out.println(\"Enter array elements:\");\r\n                for (int i = 0; i &lt; n; i++) {\r\n                    a[i] = input.nextInt();\r\n                }\r\n            }else{\r\n                \/\/quit\r\n                System.exit(0);\r\n            }\r\n\r\n            System.out.println(\"Input Array:\");\r\n            printArray(a);\r\n\r\n            System.out.println(\"Enter the value to find:\");\r\n            int search_item= input.nextInt();\r\n\r\n            for(int i=0;i&lt;n;i++){\r\n                if(search_item==a[i])\r\n                {\r\n                    index = i;\r\n                    break;\r\n                }\r\n            }\r\n\r\n            if(index == -1 ) {\r\n                System.out.println(\"Search item not found in the array\");\r\n                System.exit(0);\r\n            }\r\n            else{\r\n                System.out.println(\"Item found at position \" + index +\" \r\nmarked with *:\" );\r\n                printArray(a,index);\r\n            }\r\n\r\n        }catch (InputMismatchException ime) {\r\n            System.out.println(\"Not a valid input\");\r\n        }\r\n        catch (Exception e) {\r\n            e.printStackTrace();\r\n        } finally {\r\n            if(input!=null)\r\n            {input.close();}\r\n        }\r\n    }\r\n\r\n    private static void printArray(int[] a){\r\n        for (int anA : a) {\r\n            System.out.print(\"[ \" + anA + \" ]\");\r\n        }\r\n        System.out.println( \"\");\r\n    }\r\n\r\n    private static void printArray(int[] a, int index){\r\n        for (int i=0;i&lt; a.length;i++) {\r\n            if(i == index)\r\n                System.out.print(\"[* \" + a[i] + \"*]\");\r\n            else\r\n                System.out.print(\"[ \" + a[i] + \" ]\");\r\n        }\r\n        System.out.println( \"\");\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<h3><strong>Run output<\/strong><\/h3>\n<p>Save the program in the IDE. Execute the program and run the test cases.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1246\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Linear-Search-Java-Program.png\" alt=\"Linear Search Java Program\" width=\"1202\" height=\"711\" title=\"\"><\/p>\n<h3><strong>Testcase<\/strong><\/h3>\n<p>Enter size of the array:<br \/>\n7<br \/>\nEnter array elements:<br \/>\n23<br \/>\n56<br \/>\n89<br \/>\n99<br \/>\n156<br \/>\n1089<br \/>\n34<br \/>\nInput Array:<br \/>\n[ 23 ][ 56 ][ 89 ][ 99 ][ 156 ][ 1089 ][ 34 ]<br \/>\nEnter the value to find:<br \/>\n156<br \/>\nItem found at position 4 marked with *:<br \/>\n[ 23 ][ 56 ][ 89 ][ 99 ][* 156*][ 1089 ][ 34 ]<\/p>\n<h3><strong>Testcase<\/strong><\/h3>\n<p>Enter size of the array:<br \/>\n7<br \/>\nEnter array elements:<br \/>\n23<br \/>\n56<br \/>\n89<br \/>\n99<br \/>\n156<br \/>\n1089<br \/>\n34<br \/>\nInput Array:<br \/>\n[ 23 ][ 56 ][ 89 ][ 99 ][ 156 ][ 1089 ][ 34 ]<br \/>\nEnter the value to find:<br \/>\n1088<br \/>\nSearch item not found in the array<\/p>\n<h3><strong>Testcase<\/strong><\/h3>\n<p>Enter size of the array:<br \/>\n-123<br \/>\nEnter positive number.<\/p>\n<h3><strong>Testcase<\/strong><\/h3>\n<p>Enter size of the array:<br \/>\nstring<br \/>\nNot a valid input<\/p>\n<p>&#8212;<\/p>\n<p>Java Tutorial on this website: <strong><a href=\"https:\/\/www.testingdocs.com\/java-tutorial\/\">https:\/\/www.testingdocs.com\/java-tutorial\/<\/a><\/strong><\/p>\n<p>For more information on Java, visit the official website :<\/p>\n<p><strong><a href=\"https:\/\/www.oracle.com\/in\/java\/\" rel=\"noopener\">https:\/\/www.oracle.com\/in\/java\/<\/a><\/strong><\/p>\n<!--themify_builder_content-->\n<div id=\"themify_builder_content-1327\" data-postid=\"1327\" class=\"themify_builder_content themify_builder_content-1327 themify_builder tf_clear\">\n    <\/div>\n<!--\/themify_builder_content-->\n","protected":false},"excerpt":{"rendered":"<p>Problem statement Write a java program for linear search of an unsorted array. Run your program with a few testcases do demonstrate your program. An unsorted or unordered array is an array where elements of the array are not sorted. To search for an element in this array, you have to scan the entire array [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-1327","post","type-post","status-publish","format-standard","hentry","category-java-programs","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1327","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=1327"}],"version-history":[{"count":9,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1327\/revisions"}],"predecessor-version":[{"id":21032,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1327\/revisions\/21032"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=1327"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=1327"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=1327"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}