Java How can I add an integer to a sorted array using binary search in Java?

AI Thread Summary
The discussion revolves around implementing a method to insert an integer into a sorted array while maintaining its order. The initial code provided is a binary search method to find the position of the integer. Suggestions include using a temporary variable to shift elements in a static array or creating a new dynamic array if necessary. For dynamic data structures, alternatives like the Vector or ArrayList classes in Java are recommended, with a preference for ArrayList due to its modern usage. The conversation also touches on the potential benefits of coding the binary search as a recursive function for educational purposes. Additionally, it is clarified that when using generics in Java, primitives cannot be used directly, thus emphasizing the correct usage of Integer instead of int in collections.
pumas
Messages
15
Reaction score
0
Hello,

I need to write a method that adds an interger to a sorted array of integers. I'm using the following code to search for an interger in the array

private static int binarySearch(int[] list, int key,
int low, int high) {
while (low <= high) {
int mid = (low + high) / 2;
int midVal = list[mid];

if (midVal < key) {
low = mid + 1;
} else if (midVal > key) {
high = mid - 1;
} else {
return mid; // key found
}
}
return -(low + 1); // key not found.
}

I have not done programming in a long time, so I'm very confused on how to write the code to insert a number and preserved the sorted order.
I would appreciate any help

Thanks :smile:
 
Technology news on Phys.org
You could search for the next largest number, store it in a temporary variable and:

1. Replace the number you just stored in the temp variable with the one you want to insert.
2. Use the temporary variable to shift the array down by one.

Eg:
for(i=maxval;i>x;i--) /*x is the position of the next largest number maxval is the largest index number of the array*/

{
arr[x-1]=arr[x];
}
arr=j; //j is the number you want to insert and i=x now

This is good enough for a static array, but for a dynamic one i guess you'd need to create a new array with one more element. I can't help you in the actual coding cause I only know c++. The logic, however, should be the same.
 
He wants to add an item to a list, not delete it, so shouldn't that be:

arr[x+1]=arr[x];

?
 
Last edited:
Thanks for your help
 
Jeff Reid said:
He wants to add an item to a list, not delete it, so shouldn't that be:

arr[x+1]=arr[x];

?

Since the array is static, one item will be deleted. If the array is dynamic, and you want to add an item in the middle, then you use the linked list approach, which is totally different.
 
If you're not required to use an array as your data structure then you should use a Vector instead, which is basically an array that automatically grows when necessary.

Also, if this is a school assignment then you might get extra points if you code your binary search as a recursive function.
 
Last edited:
-Job- said:
If you're not required to use an array as your data structure then you should use a Vector instead, which is basically an array that automatically grows when necessary.

Also, if this is a school assignment then you might get extra points if you code your binary search as a recursive function.

How would you do that (the vector part)?
 
-Job- said:
Use the Vector class:
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Vector.html
Code:
Vector<int> MyVector = new Vector<int>(initialSize)
//add to vector:
MyVector.add(200);
//or insert at location:
MyVector.insertElementAt(232, 0);

The Vector automatically grows.

Vector<int> MyVector is not a valid code

Vector<Integer> is valid, since you cannot pass primitives into the <> tags.

It is generally recommended to use ArrayList instead of Vector class.

That is one can do

ArrayList<Integer> integerList = new ArrayList<Integer>();
integerList.add(5);
integerList.add(6);
...add as many as you like the list will grow as needed.
 
  • #10
haki said:
Vector<int> MyVector is not a valid code
Vector<Integer> is valid, since you cannot pass primitives into the <> tags.

Sorry, I've been doing C# exclusively for the past 6 months and i use primitives in generic collections all the time.
 
Back
Top