Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
LinearSearch
import java.util.ArrayList; import java.util.Random; import java.util.Scanner; // SEARCHER CLASS (THREAD) class Searcher extends Thread { private ArrayList<Integer> f; private int a, b; private int x; boolean found; Searcher(ArrayList<Integer> f1, int a, int b, int x) { f = f1; this.a = a; this.b = b; this.x = x; } public void run() { int k = a; found = false; while(k < b && !found) { if(f.get(k) == x) { found = true; } k++; } } boolean getResult() { return found; } } public class LinearSearch { public static void main(String args[]) { // GENERATE RANDOM ARRAY OF 100 INTS BETWEEN 0 AND 10 ArrayList<Integer> list = new ArrayList<Integer>(100); Random random = new Random(); for (int i = 0; i < 100; i++) { list.add(random.nextInt(10)); } // REQUEST USER TO NUMBER TO LOOK FOR Scanner scanner = new Scanner(System.in); System.out.print("Number to look for: "); int x = scanner.nextInt(); scanner.close(); // GET DATA AND VALUE FOR X Searcher t1 = new Searcher(list, 0, 50, x); Searcher t2 = new Searcher(list, 50, 100, x); // START BOTH THREADS t1.start(); t2.start(); try { t1.join(); t2.join(); } catch(InterruptedException e) { // THROW EXCEPTION } boolean found = t1.getResult() || t2.getResult(); System.out.println("Found = " + found); } }
run
|
edit
|
history
|
help
0
Rotate matrix clockwise
1.5
3rd
2A
Array - char
program
jb11.0 threads tick tock 1.0
3.D
LeetCode 121 - Best time to buy and sell stock - O(n) solution
Problem: fb_series