Sha256: 566ea9fa2e3aa6735ff4dafe25e2395e225f53e373e1580a1c2e8900f1d38b16

Contents?: true

Size: 932 Bytes

Versions: 237

Compression:

Stored size: 932 Bytes

Contents

import java.util.List;

public class BinarySearch<T extends Comparable<T>> {

    private List<T> array;
    private int arraySize;

    public BinarySearch(List<T> array) {
        this.array = array;
        this.arraySize = array.size();
    }

    public int indexOf(T value) {
        return search(value);
    }

    public List<T> getArray() {
        return array;
    }

    private int search(T value) {
        int left = 0;
        int right = this.arraySize - 1;
        int middle;
        T element;
        while (left <= right) {
            middle = (int) Math.floor(0.5 * (left + right));
            element = this.array.get(middle);
            if (value.compareTo(element) > 0) {
                left = middle + 1;
            } else if (value.compareTo(element) < 0) {
                right = middle - 1;
            } else {
                return middle;
            }
        }
        return -1;
    }
}

Version data entries

237 entries across 237 versions & 1 rubygems

Version Path
trackler-2.2.1.47 tracks/java/exercises/binary-search/.meta/src/reference/java/BinarySearch.java
trackler-2.2.1.46 tracks/java/exercises/binary-search/.meta/src/reference/java/BinarySearch.java
trackler-2.2.1.45 tracks/java/exercises/binary-search/.meta/src/reference/java/BinarySearch.java
trackler-2.2.1.44 tracks/java/exercises/binary-search/.meta/src/reference/java/BinarySearch.java
trackler-2.2.1.43 tracks/java/exercises/binary-search/.meta/src/reference/java/BinarySearch.java
trackler-2.2.1.42 tracks/java/exercises/binary-search/.meta/src/reference/java/BinarySearch.java
trackler-2.2.1.41 tracks/java/exercises/binary-search/.meta/src/reference/java/BinarySearch.java
trackler-2.2.1.40 tracks/java/exercises/binary-search/.meta/src/reference/java/BinarySearch.java
trackler-2.2.1.39 tracks/java/exercises/binary-search/.meta/src/reference/java/BinarySearch.java
trackler-2.2.1.38 tracks/java/exercises/binary-search/.meta/src/reference/java/BinarySearch.java
trackler-2.2.1.37 tracks/java/exercises/binary-search/.meta/src/reference/java/BinarySearch.java
trackler-2.2.1.36 tracks/java/exercises/binary-search/.meta/src/reference/java/BinarySearch.java
trackler-2.2.1.35 tracks/java/exercises/binary-search/.meta/src/reference/java/BinarySearch.java
trackler-2.2.1.34 tracks/java/exercises/binary-search/.meta/src/reference/java/BinarySearch.java
trackler-2.2.1.33 tracks/java/exercises/binary-search/.meta/src/reference/java/BinarySearch.java
trackler-2.2.1.32 tracks/java/exercises/binary-search/.meta/src/reference/java/BinarySearch.java
trackler-2.2.1.31 tracks/java/exercises/binary-search/.meta/src/reference/java/BinarySearch.java
trackler-2.2.1.30 tracks/java/exercises/binary-search/.meta/src/reference/java/BinarySearch.java
trackler-2.2.1.29 tracks/java/exercises/binary-search/.meta/src/reference/java/BinarySearch.java
trackler-2.2.1.28 tracks/java/exercises/binary-search/src/example/java/BinarySearch.java