Java-программа, которая использует методы из уникального класса, работает неправильно - PullRequest
0 голосов
/ 05 марта 2019

Я создал класс BinarySearch, который имеет три различные реализации двоичного поиска.Я также создал бинарную тестовую программу, которая тестирует различные реализации и записывает результаты в выходной файл.Проблема, с которой я столкнулся, заключается в том, что программа случайным образом приостанавливает выполнение метода runSearch в моей программе, и мне приходится принудительно останавливать его.

Класс BinarySearch:

import java.util.*;

public class BinarySearch {
    private int [] A;
    private int k;
    public BinarySearch(int [] A, int k) {
        this.A = A;
        this.k = k;
    }

    public static int binBasic (int [] A, int K) {
        // Return the  amount of iterations it took to find k in A uses an integer count to keep track of the iterations
        int count = 0;
        int l = -1;
        int r = A.length;
        // l and r are beyond array bounds
        while (l+1 != r) {
            // Stop when l and r meet
            int i = (l+r)/2; // Check middle of remaining subarray
            if (K == A[i]) return count;
            // Found it
            if (K < A[i]) {
                r = i;
                // In left half
                count++;
            }
            if (K > A[i]) {
                l = i;
                // In right half
                count++;
            }
        }
        return -1;
        // Search value not in A
    }

    public static int bin3 (int [] A, int K) {
        // Return the  amount of iterations it took to find k in A uses an integer count to keep track of the iterations
        int count = 0;
        int l = -1;
        int r = A.length;
        // l and r are beyond array bounds
        while (l+1 != r) {
            // Stop when l and r meet
            int i = l + ((r-l)/3);
            if (K == A[i]) return count;
            // Found it
            if (K < A[i]) {
                r = i;
                // In left half
                count++;
            }
            if (K > A[i]){
                l = i;
                // In right half
                count++;
            }
        }
        return -1;
        // Search value not in A
    }

    public static int binMin2 (int [] A, int K) {
        // Return the  amount of iterations it took to find k in A uses an integer count to keep track of the iterations
        int count=0;
        int l = -1;
        int r = A.length;
        // l and r are beyond array bounds
        while (l+1 != r) {
            // Stop when l and r meet
            int i = r-2;
            if (K == A[i]) return count;
            // Found it
            if (K < A[i]) {
                r = i;
                // In left half
                count++;
            }
            if (K > A[i]) {
                l = i;
                // In right half
                count++;
            }
        }
        return -1;
        // Search value not in A
    }

}

Метод двоичного теста:

//runs the three iterations of binarysearch and writes the results into BinarySearchResults.txt
public static void runSearch(int [] A, int k)throws FileNotFoundException {
    BinarySearch x = new BinarySearch(A,k);
    out1.println("The amount of iterations it took to find k in binBasic was: " + x.binBasic(A,k));
    out1.println("The amount of iterations it took to find k in bin3 was: " + x.bin3(A,k));
    out1.println("The amount of iterations it took to fins k in binMin was: " + x.binMin2(A,k));
    out1.println();
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...