forked from ghostmkg/programming-language
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinear_Search_Algorithm.java
73 lines (56 loc) · 2.24 KB
/
Linear_Search_Algorithm.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.util.Scanner;
public class LinearSearchAlgorithm {
/**
* This method performs a linear search on an array to find the target element.
*
* @param arr The array to search through.
* @param target The element to search for.
* @return The index of the target element if found, otherwise -1.
*/
public static int linearSearch(int[] arr, int target) {
// Loop through each element in the array
for (int i = 0; i < arr.length; i++) {
// Check if the current element matches the target
if (arr[i] == target) {
return i; // Return the index if target is found
}
}
return -1; // Return -1 if the target is not found
}
public static void main(String[] args) {
// Create a Scanner object to take input from the user
Scanner scanner = new Scanner(System.in);
// Take the size of the array as input from the user
System.out.print("Enter the size of the array: ");
int size = scanner.nextInt();
// Create an array of the given size
int[] numbers = new int[size];
// Take the array elements as input from the user
System.out.println("Enter " + size + " elements for the array:");
for (int i = 0; i < size; i++) {
numbers[i] = scanner.nextInt();
}
// Take the target element to search for as input from the user
System.out.print("Enter the target element to search for: ");
int target = scanner.nextInt();
// Perform linear search
int result = linearSearch(numbers, target);
// Output the result
if (result != -1) {
System.out.println("Element " + target + " found at index " + result);
} else {
System.out.println("Element " + target + " not found in the array.");
}
// Close the scanner
scanner.close();
}
}
// Time Complexity:
// Best Case: O(1)
// If the target is the first element.
// Worst Case: O(n)
// If the target is the last element or not in the array.
// Space Complexity:
// O(1)
// Since we are using a fixed amount of extra space regardless of the input size.
// The input array does not count as extra space.