Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
57 lines (50 loc) · 1.64 KB

File metadata and controls

57 lines (50 loc) · 1.64 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package Searches;
import java.util.Random;
import java.util.stream.Stream;
/**
* Linear search is the easiest search algorithm It works with sorted and unsorted arrays (an binary
* search works only with sorted array) This algorithm just compares all elements of an array to
* find a value
*
* <p>Worst-case performance O(n) Best-case performance O(1) Average performance O(n) Worst-case
* space complexity
*
* @author Varun Upadhyay (https://github.com/varunu28)
* @author Podshivalov Nikita (https://github.com/nikitap492)
* @see BinarySearch
* @see SearchAlgorithm
*/
public class LinearSearch implements SearchAlgorithm {
/**
* Generic Linear search method
*
* @param array List to be searched
* @param value Key being searched for
* @return Location of the key
*/
@Override
public <T extends Comparable<T>> int find(T[] array, T value) {
for (int i = 0; i < array.length; i++) {
if (array[i].compareTo(value) == 0) {
return i;
}
}
return -1;
}
public static void main(String[] args) {
// just generate data
Random r = new Random();
int size = 200;
int maxElement = 100;
Integer[] integers =
Stream.generate(() -> r.nextInt(maxElement)).limit(size).toArray(Integer[]::new);
// the element that should be found
Integer shouldBeFound = integers[r.nextInt(size - 1)];
LinearSearch search = new LinearSearch();
int atIndex = search.find(integers, shouldBeFound);
System.out.println(
String.format(
"Should be found: %d. Found %d at index %d. An array length %d",
shouldBeFound, integers[atIndex], atIndex, size));
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.