Java Arrays insert element using the binary search result value
import java.util.Arrays; public class Main { public static void main(String[] args) { int[] array = new int[10]; insertInOrder(array,9);//from w w w .ja v a 2 s . c o m insertInOrder(array,6); insertInOrder(array,17); insertInOrder(array,28); insertInOrder(array,-1); insertInOrder(array,19); insertInOrder(array,7); insertInOrder(array,12); System.out.println(Arrays.toString(array)); } static int elements = 0; public static void insertInOrder(int[] array, int n) { int pos = Arrays.binarySearch(array, 0, elements, n); if (pos < 0) pos = ~pos; if (pos < elements) System.arraycopy(array, pos, array, pos + 1, elements - pos); array[pos] = n; elements++; } }