0

I'm trying to implement a binary insert method.

Currently this method is very simple, it takes an argument, in a while loop it searches for an element that is bigger than the argument (in this case a String that is the last name of a person), it breaks once it finds it and shifts the rest of the array to the right. Then the argument is inserted at the position of breaking.

I tried changing it to one that would search for the insert position by borrowing from the binary search algorithm. However, I just can't get it to work.

Could you please let me know what I'm doing wrong? Here's my code:

public void insert(Person person) 
{

String lastName = person.getLastName(); int position = -1; // the position from which the array will be shifted to the right and where the argument will be inserted, will be assigned properly below int lowerBound = 0; int upperBound = numElems - 1; int currIt; if (numElems == 0) array[0] = person; // if array empty insert at first position else { while (true) { currIt = (lowerBound + upperBound) / 2; //the item to compare with int result2 = 0; int result1 = array[currIt].getLastName().compareTo(lastName); if (array[currIt+1] != null) // if the next element is not null, compare the argument to it result2 = array[currIt+1].getLastName().compareTo(lastName); if (currIt == 0 && result1 > 0) // this is when the argument is smaller then the first array element { position = 0; break; } // if the position to insert is the last one, or we have found a suitable position between a smaller and a bigger element else if ( (result1 < 0 && (currIt == numElems-1)) || (result1 < 0 && result2 > 0) ) { position = currIt+1; break; } else { if (result1 < 0) // the place to put it should be in the upper half lowerBound = currIt + 1; else upperBound = currIt - 1; //in the lower half } } } // position has been set to anything else other -1, then we have found our spot, probably a redundant check but useful for debugging if (position != -1) { //shift array to the right and insert element for (int j = numElems; j > position; j--) array[j] = array[j-1]; System.out.println("Inserted an element"); array[position] = person; numElems++; } }

2
  • Can you provide the simplest example which demonstrates your problem? Commented Sep 6, 2012 at 13:17
  • If say we have the following: arr.insert(new Person("Charles", "Dickens", 23));arr.insert(new Person("Adam", "Bay", 29)); arr.insert(new Person("Ethan", "Fowler", 18)); I was hoping to arrange them by last name ("Bay"...); This works fine: String lastName = person.getLastName(); int i; for (i = 0; i < numElems; i++) if (array[i].getLastName().compareTo(lastName) > 0) break; for (int j = numElems; j > i; j--) array[j] = array[j-1]; array[i] = person; numElems++; But wanted to speed it up:) Commented Sep 6, 2012 at 13:33

1 Answer 1

1

Your "probably [] redundant check" prohibits initial inserting. Position is -1 the first time.

Setting position to 0 at the top, should fix the problem.

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

Comments

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.