Kata 2 - Binary Search (part 2)

, code kata

For today my implementation is:

public class BinarySearch {
  private Integer m_target;
  private Integer m_tracker;
  private Integer m_searchPoint;
  private Boolean m_found = false;
  public Integer BinaryChop(Integer target, Integer[] chopArray) {
    m_target = target;
    m_searchPoint = chopArray.size()/2;
    m_tracker = searchPoint;
    Integer[] tempArray = new Integer[]();
    while(chopArray.size() != 1) {
      tempArray = search(m_searchPoint, chopArray);
      chopArray.clear();
      chopArray = tempArray;
      tempArray.clear();
    }
  }
  private boolean search(Integer position, Integer[] searchArray) {
    if(searchArray[position] == m_target) {
      return new Integer[]{position};
    } else {
      if(searchArray[position] > m_target) {
        return SplitArray(0,position-1,searchArray);
      } else {
        return SplitArray(position+1,searchArry.size()+1,searchArray);
      }
    }
  }
  private Integer[] SplitArray(Integer startPoint, Integer endPoint, Integer[] oldArray) {
    Integer[] newArray = new Integer[]();
    for(Integer i = startPoint; i <= endPoint; i++) {
      newArray.add(oldArray[i]);
    }
    return newArray;
  }
}
Share on Twitter, Facebook, Google+
Prev Next