Why my implementation of binary search in Java slower than sequential search? - java

I'm asking because I basically just translate my python code to java and when I test in python the result is as expected (binary search faster than sequential search). It is not the case in Java though. Why is my binary search so inefficient?
import java.util.Arrays;
import java.util.stream.IntStream;
public class Chap5exe1 {
private static boolean sequential_search(int[] iterable, int item){
boolean found = false;
boolean stop = false;
int current_position = 0;
while ((current_position < iterable.length) & (!found) & (!stop)){ //!found is found == false
if (iterable[current_position] == item){
found = true;
}
else if (iterable[current_position] > item){
stop = true;
}
else {
current_position += 1;
}
}
return found;
}
private static boolean binary_search(int[] iterable, int item) {
if (iterable.length == 0) {
return false;
}
int mid_point = iterable.length / 2;
if (iterable[mid_point] == item){
return true;
}
else if (iterable[mid_point] > item){
return binary_search(Arrays.copyOfRange(iterable, 0, mid_point), item);
}
else {
return binary_search(Arrays.copyOfRange(iterable, mid_point+1, iterable.length), item);
}
}
public static void main(String[] args) {
for(int i = 1000; i <= 10000000; i += 1000){
int[] list = IntStream.range(0, i).toArray();
long startTime1 = System.nanoTime();
sequential_search(list, i);
long stopTime1 = System.nanoTime();
long startTime2 = System.nanoTime();
binary_search(list, i);
long stopTime2 = System.nanoTime();
String output = String.format("Sequential search:%d / Binary search:%d", stopTime1-startTime1, stopTime2-startTime2);
System.out.println(output);
}
}}

As said in comments, the Arrays.copyOfRange() method call causes the overhead. Try using the following code instead:
private static boolean binary_search(int[] iterable, int item) {
if (iterable.length == 0) {
return false;
}
return binary_search(iterable, item, 0, iterable.length);
}
private static boolean binary_search(int[] iterable, int item, int start, int end) {
int mid_point = (start + end) / 2;
if (iterable[mid_point] == item){
return true;
} else if (start == end) {
return false;
}
else if (iterable[mid_point] > item){
return binary_search(iterable, item, start, mid_point);
}
else {
return binary_search(iterable, item, mid_point+1, end);
}
}

Related

return statement in boolean method

I do not understand what I should return. My method returns false if the last time it goes
through the for-loop it is false. If the last time is true than it returns true. But I want it to return false regardless of where the false occurred.
public class test {
public static void main(String[] args) {
int number = 4;
int[] intArray = {4, 8, 12, 16};
System.out.println(allMultipleOf(intArray, number));
}
public static boolean allMultipleOf(int[] ary, int n){
boolean a = true;
for(int i = 0; i < ary.length; i++){
if(ary[i] % n == 0){
a = true;
//System.out.println(a);
break;
} else {
a = false;
}
}
}
return a; //what should I return
}
You can return false early from the method, if we reached the end without returning false, then return true:
public static boolean allMultipleOf(int[] ary, int n) {
for (int i = 0; i < ary.length; i++) {
if (ary[i] % n != 0) {
return false;
}
}
return true;
}
It should return false, the default of the boolean is print at this place.
You can make the return statement within the method. it return true.
public static boolean allMultipleOf(int[] ary, int n) {
boolean a = true;
for(int i = 0; i < ary.length; i++) {
if (ary[i] % n == 0) {
a = true;
//System.out.println(a);
break;
} else {
a = false;
}
}
return a;
}

Autocomplete byReverseWeightOrder comparator issue

I have been working on this problem for several hours now and I just cannot figure out what I am doing wrong here. Could anyone help point me in the right direction?
I was asked to write an Autocomplete program and I've completed everything except for this one method I cannot get working. Each term has: 1. String query and 2. long weight.
Here is the method:
public static Comparator<Term> byReverseWeightOrder() {
return new Comparator<Term>() { // LINE CAUSING PROBLEM
public int compare(Term t1, Term t2) {
if (t1.weight > t2.weight) { // LINE CAUSING PROBLEM
return -1;
} else if (t1.weight == t2.weight) {
return 0;
} else {
return 1;
}
}
};
}
My problem is that no matter how I mess with the method I always result in a NullPointerException(). Which, it points to this method (byReverseWeightOrder) as well as these two statements.
Arrays.sort(matches, Term.byReverseWeightOrder());
Term[] results = autocomplete.allMatches(prefix);
Here is the rest of the code if it can be found helpful:
Term
import java.util.Comparator;
public class Term implements Comparable<Term> {
public String query;
public long weight;
public Term(String query, long weight) {
if (query == null) {
throw new java.lang.NullPointerException("Query cannot be null");
}
if (weight < 0) {
throw new java.lang.IllegalArgumentException("Weight cannot be negative");
}
this.query = query;
this.weight = weight;
}
public static Comparator<Term> byReverseWeightOrder() {
return new Comparator<Term>() {
public int compare(Term t1, Term t2) {
if (t1.weight > t2.weight) {
return -1;
} else if (t1.weight == t2.weight) {
return 0;
} else {
return 1;
}
}
};
}
public static Comparator<Term> byPrefixOrder(int r) {
if (r < 0) {
throw new java.lang.IllegalArgumentException("Cannot order with negative number of characters");
}
final int ref = r;
return
new Comparator<Term>() {
public int compare(Term t1, Term t2) {
String q1 = t1.query;
String q2 = t2.query;
int min;
if (q1.length() < q2.length()) {
min = q1.length();
}
else {
min = q2.length();
}
if (min >= ref) {
return q1.substring(0, ref).compareTo(q2.substring(0, ref));
}
else if (q1.substring(0, min).compareTo(q2.substring(0, min)) == 0) {
if (q1.length() == min) {
return -1;
}
else {
return 1;
}
}
else {
return q1.substring(0, min).compareTo(q2.substring(0, min));
}
}
};
}
public int compareTo(Term that) {
String q1 = this.query;
String q2 = that.query;
return q1.compareTo(q2);
}
public long getWeight() {
return this.weight;
}
public String toString() {
return this.weight + "\t" + this.query;
}
}
BinarySearchDeluxe
import java.lang.*;
import java.util.*;
import java.util.Comparator;
public class BinarySearchDeluxe {
public static <Key> int firstIndexOf(Key[] a, Key key, Comparator<Key> comparator) {
if (a == null || key == null || comparator == null) {
throw new java.lang.NullPointerException();
}
if (a.length == 0) {
return -1;
}
int left = 0;
int right = a.length - 1;
while (left + 1 < right) {
int middle = left + (right - left)/2;
if (comparator.compare(key, a[middle]) <= 0) {
right = middle;
} else {
left = middle;
}
}
if (comparator.compare(key, a[left]) == 0) {
return left;
}
if (comparator.compare(key, a[right]) == 0) {
return right;
}
return -1;
}
public static <Key> int lastIndexOf(Key[] a, Key key, Comparator<Key> comparator) {
if (a == null || key == null || comparator == null) {
throw new java.lang.NullPointerException();
}
if (a == null || a.length == 0) {
return -1;
}
int left = 0;
int right = a.length - 1;
while (left + 1 < right) {
int middle = left + (right - left)/2;
if (comparator.compare(key, a[middle]) < 0) {
right = middle;
} else {
left = middle;
}
}
if (comparator.compare(key, a[right]) == 0) {
return right;
}
if (comparator.compare(key, a[left]) == 0) {
return left;
}
return -1;
}
}
AutoComplete
import java.util.Arrays;
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.util.Comparator;
public class Autocomplete {
public Term[] terms;
public Autocomplete(Term[] terms) {
if (terms == null) {
throw new java.lang.NullPointerException();
}
this.terms = terms.clone();
Arrays.sort(this.terms);
}
public Term[] allMatches(String prefix) {
if (prefix == null) {
throw new java.lang.NullPointerException();
}
Term theTerm = new Term(prefix, 0);
int start = BinarySearchDeluxe.firstIndexOf(terms, theTerm, Term.byPrefixOrder(prefix.length()));
int end = BinarySearchDeluxe.lastIndexOf(terms, theTerm, Term.byPrefixOrder(prefix.length()));
int count = start;
System.out.println("Start: " + start + " End: " + end);
if (start == -1 || end == -1) {
// System.out.println("PREFIX: " + prefix);
throw new java.lang.NullPointerException();
} // Needed?
Term[] matches = new Term[end - start + 1];
//matches = Arrays.copyOfRange(terms, start, end);
for (int i = 0; i < end - start; i++) {
matches[i] = this.terms[count];
count++;
}
Arrays.sort(matches, Term.byReverseWeightOrder());
System.out.println("Finished allmatches");
return matches;
}
public int numberOfMatches(String prefix) {
if (prefix == null) {
throw new java.lang.NullPointerException();
}
Term theTerm = new Term(prefix, 0);
int start = BinarySearchDeluxe.firstIndexOf(terms, theTerm, Term.byPrefixOrder(prefix.length()));
int end = BinarySearchDeluxe.lastIndexOf(terms, theTerm, Term.byPrefixOrder(prefix.length()));
System.out.println("Finished numberMatches");
return end - start + 1; // +1 needed?
}
public static void main(String[] args) throws IOException {
// Read the terms from the file
Scanner in = new Scanner(new File("wiktionary.txt"));
int N = in.nextInt(); // Number of terms in file
Term[] terms = new Term[N];
for (int i = 0; i < N; i++) {
long weight = in.nextLong(); // read the next weight
String query = in.nextLine(); // read the next query
terms[i] = new Term(query.replaceFirst("\t",""), weight); // construct the term
}
Scanner ip = new Scanner(System.in);
// TO DO: Data Validation Here
int k;
do {
System.out.println("Enter how many matching terms do you want to see:");
k = ip.nextInt();
} while (k < 1 || k > N);
Autocomplete autocomplete = new Autocomplete(terms);
// TO DO: Keep asking the user to enter the prefix and show results till user quits
boolean cont = true;
do {
// Read in queries from standard input and print out the top k matching terms
System.out.println("Enter the term you are searching for. Enter * to exit");
String prefix = ip.next();
if (prefix.equals("*")) {
cont = false;
break;
}
Term[] results = autocomplete.allMatches(prefix);
System.out.println(results.length);
for(int i = 0; i < Math.min(k,results.length); i++)
System.out.println(results[i].toString());
} while(cont);
System.out.println("Done!");
}
}
I apologize for the sloppy code, I have been pulling my hair out for awhile now and keep forgetting to clean it up.
Two examples:
Example 1:
int k = 2;
String prefix = "auto";
Enter how many matching terms do you want to see:
2
Enter the term you are searching for. Enter * to exit
auto
619695 automobile
424997 automatic
Example 2:
int k = 5;
String prefix = "the";
Enter how many matching terms do you want to see:
5
Enter the term you are searching for. Enter * to exit
the
5627187200 the
334039800 they
282026500 their
250991700 them
196120000 there

8 puzzle iterative deep search implementation

I have implemented a depth first search (recursive) for the 8 puzzle problem in Java:
protected PuzzleState depthFirstSearch(PuzzleState state) {
PuzzleState start = this.getStartState();
PuzzleState goal = this.getGoalState();
PuzzleState stop = null;
int limit = 35;
int depth = state.getDepth();
boolean tooDeep = false;
if (state.equals(goal)) {
return state;
} else {
if (depth == limit) {
return stop;
} else {
Collection<Integer> actions = PuzzleAction.getPuzzleActions();
for (Integer action : actions) {
PuzzleState starter = start;
PuzzleState next = state.succ(action);
if (next != null) {
starter = depthFirstSearch(next);
}
if (starter == stop) {
tooDeep = true;
} else {
if (!starter.equals(start)) {
return starter;
}
}
}
}
}
if (tooDeep)
return stop;
else
return start;
}
I don't know what I have to change to transform it to a iterative deepening depth search. I know that there is no limit for the depth, because it increases in every round.
Tried this:
protected PuzzleState iterativeDeepSearch(PuzzleState state) {
int depth = state.getDepth();
for(int limit = 1; limit < depth; limit ++){
depthFirstSearch(state, limit);
}
}
Does anyone know how to change it to the needed IDS?
Thank you in advance!

Backward Recursive Linear Search

I am attempting to write a function that finds the last occurrence of a target in a vector by modifying a linear search function.
private int linearSearchRecursive(int[] input, int key,int index) {
if (index == 0) {
return -1;
}
if (input[index] == key) {
return index;
}
else
return linearSearchRecursive(input,key,--index);
}
I thought of a way to make it work by using a helper function...
public static int findLastOccurance(int[] items, int key){
return linearSearchRecursive(items, key, items.length - 1);
}
Or something of that nature, but was wondering if there was an easier way where I could use only one function but keep the recursiveness?
Not easier but only one function:
public class Test {
public static int findLastOccuranceRecursive(int[] input, int key, int... optionalIndex) {
if (optionalIndex.length == 0) {
optionalIndex = new int[] { input.length - 1 };
} else if (optionalIndex.length != 1) {
throw new IllegalArgumentException("size of optionalIndex must be 0 or 1");
}
if (optionalIndex[0] == 0) {
return -1;
}
if (input[optionalIndex[0]] == key) {
return optionalIndex[0];
} else {
optionalIndex[0]--;
return findLastOccuranceRecursive(input, key, optionalIndex);
}
}
public static int findLastOccuranceIterative(int[] items, int key) {
for (int i = items.length - 1; i >= 0; i--) {
if (items[i] == key) {
return i;
}
}
return -1;
}
public static void main(String[] args) {
int[] input = { 1, 1, 1, 2, 1, 2, 1, 1 };
int testRecursive = findLastOccuranceRecursive(input, 2);
int testIterative = findLastOccuranceIterative(input, 2);
System.out.println("testRecursive: " + testRecursive + " testIterative: " + testIterative);
}
}

Recursive method checking whether a row of integers is descending: return true/false

I have to write a recursive method in Java that returns true if a row is descending and false it does not.
This is what I tried, but it doesn't work properly:
ArrayList<Integer> getallen = new ArrayList();
getallen.add(500);
getallen.add(400);
getallen.add(300);
getallen.add(200);
getallen.add(100);
getallen.add(0);
System.out.println(isDescending(getallen));
}
public static boolean isDescending(ArrayList<Integer> getallen) {
if (getallen.size() >= 2) {
if (getallen.get(0) < getallen.get(1)) {
return false;
} else if (getallen.size() > 0) {
getallen.remove(0);
return isDescending(getallen);
} else {
return true;
}
} else {
return false;
}
}
I think you have unnecessary cases if the size is less than 2 you can only assume true.
Try:
public static boolean isDescending(ArrayList<Integer> getallen) {
if (getallen.size() >= 2) {
if (getallen.get(0) < getallen.get(1)) {
return false;
} else {
getallen.remove(0);
return isDescending(getallen);
}
} else {
return true;
}
}
If I had to grade this, it would get a big fat X for
Having been fraudulently asked on stackoverflow
Being quite inefficient (try running this test on a list of a million elements, then realise that removing element 0 in an ArrayList causes all elements to shift down)
Instead consider:
public static boolean isDescending(List<Integer> getallen) {
return isDescending(getallen, 0);
}
public static boolean isDescending(List<Integer> getallen, int from) {
return from >= getallen.size() - 1
|| getallen.get(from) < getallen.get(from + 1)
&& isDescending(getallen, from + 1);
}
How about little bit more efficient approach with logarithmic recursion depth? Just as an exercise.
public static void main(String[] args) {
List<Integer> getallen = new ArrayList<Integer>();
getallen.add(500);
getallen.add(400);
getallen.add(300);
getallen.add(200);
getallen.add(100);
getallen.add(0);
System.out.println(isDescending(getallen));
}
public static boolean isDescending(List<Integer> getallen) {
return isDescending(getallen, 0, getallen.size());
}
private static boolean isDescending(List<Integer> getallen,
int start, int end) {
if (end - start <= 1)
return true;
if (end - start == 2) {
return getallen.get(start) > getallen.get(start + 1);
}
int middle = (start + end - 1) / 2 + 1;
return (getallen.get(middle - 1) > getallen.get(middle)) &&
isDescending(getallen, start, middle) &&
isDescending(getallen, middle, end);
}

Categories

Resources