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
Update LinearSearchThread.java
  • Loading branch information
yanglbme authored Nov 25, 2021
commit 0c9bc18510c5ee687e3fb6182aecfefb1cdf2be2
107 changes: 60 additions & 47 deletions src/main/java/com/thealgorithms/searches/LinearSearchThread.java
Original file line number Diff line number Diff line change
@@ -1,53 +1,66 @@
package search;
package com.thealgorithms.searches;

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);
}
}
import java.util.Scanner;

class Found {
private boolean found = false;
public void set(){found = true;}
public boolean found(){return found;}
public String toString(){return found+"";}
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 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;
}
}
private int[] f;
private int a, b;
private int x;
private boolean found;

Searcher(int[] f, int a, int b, int x) {
this.f = f;
this.a = a;
this.b = b;
this.x = x;
}

@Override
public void run() {
int k = a;
found = false;
while (k < b && !found) {
if (f[k] == x) {
found = true;
}
k++;
}
}

boolean getResult() {
return found;
}
}