-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathLinearsearch.java
122 lines (105 loc) · 3.42 KB
/
Linearsearch.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import java.util.ArrayList;
public class Linearsearch {
public static void main(String[] args) {
int[] nums = { 1, 2, 4,4,9,10 };
ArrayList<Integer> list = linearSearch6(nums, 4,0);
System.out.println(list);
}
// search in the array: return the index if item found
// otherwise if item not found return -1
static int linearSearch1(int[] arr, int target) {
if (arr.length == 0) {
return -1;
}
for (int index = 0; index < arr.length; index++) {
// checks for element at every index is = target
int element = arr[index];
if (element == target) {
return index;
}
}
// This will execute when target is not found
return -1;
}
// search the target and return the element
static int linearSearch2(int[] arr, int target) {
if (arr.length == 0) {
return -1;
}
for (int element : arr) {
if (element == target) {
return element;
}
}
// As -1 can be the element and in the array we return max value
return Integer.MAX_VALUE;
}
// search the target and return true or false
static boolean linearSearch3(int[] arr, int target){
if(arr.length == 0){
return false;
}
for (int index = 0; index < arr.length; index++) {
// checks for element at every index is = target
int element = arr[index];
if(element == target){
return true;
}
}
// This will execute when target is not found
return false;
}
// Using recursion
// Returning boolean value
static boolean linearSearch4(int[] arr, int target,int index){
if(index == arr.length){
return false;
}
return arr[index] == target || linearSearch4(arr, target, index+1);
}
// Returning index value
static int linearSearch5(int[] arr, int target,int index){
if(index == arr.length){
return -1;
}
if(arr[index] == target){
return index;
}
return linearSearch5(arr, target, index+1);
}
// Multiple Occurance of target in using Linear Search
static ArrayList<Integer> list = new ArrayList<>();
static void linearSearch6(int[] arr, int target, int index){
if(index == arr.length){
return;
}
if(arr[index]==target){
list.add(index);
}
linearSearch6(arr, target, index+1);
}
// ArrayList as argument
static ArrayList<Integer> linearSearch7(int[] arr, int target, int index, ArrayList<Integer> list){
if(index == arr.length){
return list;
}
if(arr[index]==target){
list.add(index);
}
return linearSearch7(arr, target, index+1, list);
}
// ArrayList without passing as argument
// not a optimized function as everytime new arraylist is created
static ArrayList<Integer> linearSearch8(int[] arr,int target, int index){
ArrayList<Integer> list = new ArrayList<>();
if(index==arr.length){
return list;
}
if(arr[index]==target){
list.add(index);
}
ArrayList<Integer> ansfrombelow = linearSearch8(arr, target, index+1);
list.addAll(ansfrombelow);
return list;
}
}