From d3d2b41f0da7afaebd64df42c12de421f568c4cc Mon Sep 17 00:00:00 2001 From: Nitish Malang <71919457+nitishmalang@users.noreply.github.com> Date: Sun, 16 Oct 2022 01:24:34 +0530 Subject: [PATCH] Create linear-search.java its about linear --- linear-search.java | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 linear-search.java diff --git a/linear-search.java b/linear-search.java new file mode 100644 index 0000000..cedac94 --- /dev/null +++ b/linear-search.java @@ -0,0 +1,44 @@ +import java.util.Scanner; + +/** + * This is an implementation of the linear search algorithm. + * @author @inforkgodara + * + */ +public class LinearSearch { + + /** + * Linear search implementation. + * @param list the array to be searched. + * @param search the element being looked for in the array. + * @return the index at which the search element was first found or -1 if not found. + */ + public static int search(int[] list, int search) { + int length = list.length; + for (int index = 0; index < length; index++) { + if (list[index] == search) { + return index; + } + } + return -1; + } + + public static void main(String[] args) { + System.out.print("Enter length of list : "); + Scanner scanner = new Scanner(System.in); + + int length = scanner.nextInt(); + int[] list = new int[length]; + + for (int index = 0; index < length; index++) { + list[index] = scanner.nextInt(); + } + + System.out.print("Enter element to search : "); + int search = scanner.nextInt(); + int elementAt = search(list, search); + + String result = elementAt == -1 ? "Element not found." : "Element is at index " + elementAt; + System.out.println(result); + } +}