Guest User

Untitled

a guest
Oct 9th, 2016
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.62 KB | None | 0 0
  1. public void insert(int value) {
  2.     int lowerBound = 0;
  3.     int upperBound = numOfElements;
  4.     int currentIndex;
  5.  
  6.     while (true) {
  7.         currentIndex = (lowerBound + upperBound) / 2;
  8.  
  9.         if (lowerBound >= upperBound) {
  10.             break;
  11.         } else {
  12.             if (value > array[currentIndex]) {
  13.                 lowerBound = currentIndex + 1;
  14.             } else {
  15.                 upperBound = currentIndex - 1;
  16.             }
  17.         }
  18.     }
  19.  
  20.     for (int k = numOfElements; k > currentIndex; k--) {
  21.         array[k] = array[k - 1];
  22.     }
  23.  
  24.     array[currentIndex] = value;
  25.     numOfElements++;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment