mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-06 00:54:32 +08:00
Add tests, remove main
in LinearSearch
(#5670)
This commit is contained in:
@ -1,8 +1,6 @@
|
||||
package com.thealgorithms.searches;
|
||||
|
||||
import com.thealgorithms.devutils.searches.SearchAlgorithm;
|
||||
import java.util.Random;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* Linear search is the easiest search algorithm It works with sorted and
|
||||
@ -36,20 +34,4 @@ public class LinearSearch implements SearchAlgorithm {
|
||||
}
|
||||
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.printf("Should be found: %d. Found %d at index %d. An array length %d%n", shouldBeFound, integers[atIndex], atIndex, size);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user