Ordered Array (i.e. in this data structure, everything is in order)
/**
* This is the array related package.
*/
package array;
import exception.ArrayException;
/**
* Demonstrates ordered array class.
* @author Jiafan Zhou
*/
public class OrderedArray
{
private long[] array;
private int nElements;
/**
* Constructor of this Ordered Array application.
* @param max the max items in this array.
*/
public OrderedArray(int max)
{
array = new long[max];
nElements = 0;
}
/**
* Insert the value into the ordered array.
* This is a linear insert. (from smallest to biggest)
* @param value the inserted value.
* @exception ArrayException if the array is full.
*/
public void insert(long value) throws ArrayException
{
if (dataSize() == arraySize())
{
throw new ArrayException("This array is full in size.");
}
int index = 0;
// find where the new element goes
for (int i = 0; i < nElements; i++)
{
if (array[i] > value)
{
index = i;
break;
}
else
{
index = nElements;
}
}
//move bigger ones up
for (int i = nElements; i > index; i--)
{
array[i] = array[i - 1];
}
//insert the new value
array[index] = value;
nElements++;
}
/**
* This is a binary search. (needs to be an ordered array)
* Try to find the specified value in this array.
* @param searchKey search value
* @return true if found, false otherwise
*/
public boolean find(long searchKey)
{
int lowerBound = 0;
int upperBound = nElements - 1;
int currentIn;
while (true)
{
currentIn = (lowerBound + upperBound) / 2;
// found the searchKey
if (array[currentIn] == searchKey)
{
return true;
}
// can't find it
else if (lowerBound > upperBound)
{
return false;
}
else
{
// it is in upper half
if (array[currentIn] < searchKey)
{
lowerBound = currentIn + 1;
}
else //it is in lower half
{
upperBound = currentIn - 1;
}
}
}
}
/**
* This is a binary search. (needs to be an ordered array)
* Try to find the specified value in this array.
* @param searchKey search value
* @return if found, return the index of the specified value.
* if not found, return -1
*/
private int findIndex(long searchKey)
{
int lowerBound = 0;
int upperBound = nElements - 1;
int currentIn;
while (true)
{
currentIn = (lowerBound + upperBound) / 2;
// found the searchKey
if (array[currentIn] == searchKey)
{
return currentIn;
}
// can't find it
else if (lowerBound > upperBound)
{
return -1;
}
else
{
// it is in upper half
if (array[currentIn] < searchKey)
{
lowerBound = currentIn + 1;
}
else //it is in lower half
{
upperBound = currentIn - 1;
}
}
}
}
/**
* Try to delete the specified value in this array.
* If the value is deleted successfully, the old value
* will be replaced as a 0.
* This delete uses a binary search to find the value first.
* @param value the value to be deleted
* @return true if deleted sucessfully, false otherwise.
*/
public boolean delete(long value)
{
int index = findIndex(value);
if (index == -1)
{
return false;
}
else
{
//move bigger ones down
for (int j = index; j < nElements - 1; j++)
{
array[j] = array[j + 1];
}
array[nElements - 1] = 0;
nElements--;
return true;
}
}
/**
* Return the size of this array.
* @return the size of this fixed array.
*/
public int arraySize()
{
return array.length;
}
/**
* Return the elements this array contained.
* @return the elements of this array contained.
*/
public int dataSize()
{
return nElements;
}
/**
* Clear all the elements in this array.
*/
public void clear()
{
for (int i = 0; i < array.length; i++)
{
array[i] = 0;
}
}
/**
* Displays array contents.
* @return displays array contents
*/
@Override
public String toString()
{
String string = "";
for (long i : array)
{
string += (i + " ");
}
return string;
}
}