Fixing ArrayOutOfBound Error in BinarySearch

  • Comp Sci
  • Thread starter pumas
  • Start date
  • Tags
    Error
In summary: Additionally, the if statement should also be changed to if (i == 0) to properly handle the case when the new value is lower than all other values. Overall, it seems like the code just needs a few small adjustments to properly add a new value to the array in sorted order and avoid any errors.
  • #1
pumas
15
0
I'm supposed to create a method that adds intergers to an array and keep it in sorted order. I'm supposed to search for the number in the array using binarySearch. I get an error that says arrayOutOfBound I don't know how to fix this. I have written the following code:
elementData is the array,

Code:
 public void add(int value) {

      int index = binarySearch(elementData,value,0, size - 1);
     
      }
      for (int i = size; i > index; i--)
          {
           elementData[i] = elementData[i - 1];
           elementData[index] = value; 
           size++;
          }
          
         }

I appreciate any help :confused:
 
Physics news on Phys.org
  • #2
I'm a little rusty in my Java,
so your algorithm there seems to move every value up in order to make a spot to place the new value in the right sorted order right?

The only thing I can see is what happens when the new value is lower than all other values? so it goes all the way to i=0, and elementData[i-1] will give try to get to elementData[-1] which would be out of the boundary of the array.

So maybe adding and if(i=0) will fix that? so you just replace the 0'th element, since it was already moved up to [1].
 
  • #3
Thanks for your help
 
  • #4
Well, if the array's length is called size, then the elements of that array range from 0 to size - 1. However, your for loop starts with i=size, meaning you're past the end of the array.

- Warren
 

1. What causes an ArrayOutOfBound Error in BinarySearch?

An ArrayOutOfBound Error in BinarySearch occurs when the search algorithm tries to access an index that is outside the bounds of the array. This can happen if the array is not sorted in ascending order or if the target element is not present in the array.

2. How do I fix an ArrayOutOfBound Error in BinarySearch?

To fix an ArrayOutOfBound Error in BinarySearch, you can check if the array is sorted before performing the search. If it is not sorted, you can use a sorting algorithm to sort the array first. You can also check if the target element is present in the array before performing the search.

3. Is it possible to prevent an ArrayOutOfBound Error in BinarySearch?

Yes, it is possible to prevent an ArrayOutOfBound Error in BinarySearch by carefully checking the bounds of the array and ensuring that the target element is present in the array before performing the search. It is also important to make sure that the array is sorted in ascending order.

4. Can a programming language affect the occurrence of an ArrayOutOfBound Error in BinarySearch?

Yes, the programming language used can affect the occurrence of an ArrayOutOfBound Error in BinarySearch. Some languages, like Java, have built-in methods for performing binary search that handle the bounds checking automatically. Other languages may require the programmer to manually check for bounds and handle the error.

5. Are there any best practices for avoiding ArrayOutOfBound Errors in BinarySearch?

Some best practices for avoiding ArrayOutOfBound Errors in BinarySearch include carefully checking the bounds of the array, ensuring that the array is sorted in ascending order, and checking if the target element is present in the array before performing the search. It is also helpful to use built-in methods or libraries for performing binary search, as they often handle bounds checking automatically. It is also important to thoroughly test the code and handle any potential edge cases to prevent errors.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
751
  • Programming and Computer Science
Replies
20
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
982
  • Engineering and Comp Sci Homework Help
Replies
1
Views
983
  • Engineering and Comp Sci Homework Help
Replies
4
Views
924
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
851
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
Back
Top