0

So this method is passed an arraylist of Occurences, which each contain a string and a frequency. The frequency is the only important part here. But what I need to do is use binary search to insert the last element in the arraylist into the sorted position. Every time I run this code, the insertion position is printed as -1. Am I missing something in my code?

I need to keep track of the indexes in the array I hit during binary search, which shouldn't be too difficult, but explains the return type.

public ArrayList<Integer> insertLastOccurrence(ArrayList<Occurrence> occs) {
    ArrayList<Integer> path = new ArrayList<Integer>();

    int targetFreq = occs.get(occs.size()-1).frequency; //gets the frequency of the thing we want to insert

    //if the array is just 1 value, don't do anything
    if(occs.size() == 1){
        return null;
    }

    int start = 0;               // The start of the search region
    int end = occs.size()-2;// The end of the search region is 1 less than the last position
    int position = -1;           // Position of the target

    // While there is still something list left to search and
    // the element has not been found
    while (start <= end && position == -1)  {
      int mid = start + (end - start) / 2;    //int mid = (start + end) / 2;  // Location of the middle
        // Determine whether the target is smaller than, greater than,
        // or equal to the middle element
        if (targetFreq < occs.get(mid).frequency)   {
        // Target is smaller; continue the left half
        end = mid - 1;
        }
        else if (targetFreq > occs.get(mid).frequency)  {
        // Target is larger, continue the right half
        start = mid + 1;
        }
        else  {
        // Found it!
        position = mid;
        }
    }
    System.out.println(position);   
    return path;
}
3
  • Is your array sorted by frequency? Binary search will only work if the array is sorted, otherwise the assumption of continue left/right is wrong. Commented Nov 19, 2013 at 23:01
  • yes. All entries minus the last one are already sorted Commented Nov 19, 2013 at 23:02
  • Your code looks correct. I can't see an obvious issue. Please dump the occs list and check the order. Maybe it is sorted descending or by the string instead of the frequency? Commented Nov 19, 2013 at 23:10

1 Answer 1

1

So, do I understand this right? You have an ArrayList that is sorted with the exception of the last element (at size()-1) and you want to find the index this element has to be inserted after to regain the sorted property?

I suppose, with the presented code, such an index is only found if the ArrayList contains another element that equals the last (to be inserted) one because position is only set to mid if targetFreq equals the frequency of one of considered elements. Since the last element is never considered (end = size()-2) it is very likely that no equal element is found.

Sign up to request clarification or add additional context in comments.

2 Comments

Oh, I see what you mean. So I would need to modify the code so it will stop when the end and start are 1 away from eachother. This would mean that the value needs to be inserted between end and start, correct?
Yes. Probably it would suffice to change the condition of the while loop and check if position is set after the loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.