Add tests, remove main in LinearSearch (#5670)

This commit is contained in:
Hardik Pawar
2024-10-11 01:42:07 +05:30
committed by GitHub
parent 401d87365e
commit b1724fa737
3 changed files with 119 additions and 18 deletions

View File

@ -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);
}
}