Skip to content

Add Happy numbers problem in "others" category #2839

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Nov 26, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Linear search using thread
  • Loading branch information
Louve Le Bronec committed Nov 23, 2021
commit d3357d011fdce93fa530d26eae81ec2daca1026c
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package search;

import java.util.*;
class LinearSearchThread{
public static void main(String args[]){
int list[] = new int[200];
for(int j = 0; j < list.length; j++) list[j] = (int)(Math.random()*100);
for(int y : list) System.out.print(y+" ");
System.out.println();
System.out.print("Enter number to search for: ");
Scanner in = new Scanner(System.in);
int x = in.nextInt();
Searcher t = new Searcher(list,0,50,x);
Searcher t1 = new Searcher(list,50,100,x);
Searcher t2 = new Searcher(list,100,150,x);
Searcher t3 = new Searcher(list,150,200,x);
t.start(); t1.start(); t2.start(); t3.start();
try{
t.join(); t1.join(); t2.join(); t3.join();
}
catch(InterruptedException e){}
boolean found = t.getResult() || t1.getResult() || t2.getResult() || t3.getResult();
System.out.println("Found = " + found);
}
}

class Found {
private boolean found = false;
public void set(){found = true;}
public boolean found(){return found;}
public String toString(){return found+"";}
}

class Searcher extends Thread {
private int f[];
private int lb, ub;
private int x;
private boolean found;
Searcher(int f1[], int a, int b, int x) {
f = f1; lb = a; ub = b; this.x = x;
}
public void run() {
int k = lb; found = false;
while (k < ub && !found){
if(f[k] == x) found = true;
k++;
}
}
boolean getResult() {
return found;
}
}