Insertion Sort Array.
/**
* This is the array related package.
*/
package array;
import exception.ArrayException;
/**
* Demonstrates the insertion sort array.
* @author Jiafan Zhou
*/
public class InsertionSortArray
{
private long[] array;
private int nElements;
/**
* Constructor of this Selection Sort Array application.
* @param max the max items in this array.
*/
public InsertionSortArray(int max)
{
array = new long[max];
nElements = 0;
}
/**
* Insert the value into the array.
* @param value the inserted value
* @exception ArrayException when array is full in size
*/
public void insert(long value) throws ArrayException
{
if (dataSize() == arraySize())
{
throw new ArrayException("This array is full in size.");
}
array[nElements++] = value;
}
/**
* This is a linear search.
* Try to find the specified value in this array.
* @param searchKey search value
* @return true if found, false otherwise
*/
public boolean find(long searchKey)
{
for (int i = 0; i < nElements; i++)
{
if (array[i] == searchKey)
{
return true;
}
}
return false;
}
/**
* Try to delete the specified value in this array.
* If the value is deleted successfully, the old value
* will be replaced as a 0.
* @param value the value to be deleted
* @return true if deleted sucessfully, false otherwise.
*/
public boolean delete(long value)
{
for (int i = 0; i < nElements; i++)
{
if (array[i] == value)
{
for (int j = i; j < nElements - 1; j++)
{
array[j] = array[j + 1];
}
array[nElements - 1] = 0;
nElements--;
return true;
}
}
return false;
}
/**
* 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;
}
/**
* This is an insertion sort(ascending).
*/
public void ascendingSort()
{
// marker is the *array index* where left is all sorted
// and right is all non-sorted elements.
for (int marker = 1; marker < nElements; marker++)
{
long temp = array[marker];
// insert the marker into the left sorted elements
int insert;
for (insert = marker; insert > 0; insert--)
{
if (array[insert - 1] > temp)
{
array[insert] = array[insert - 1];
}
else
{
break;
}
}
array[insert] = temp;
}
}
/**
* This is an insertion sort(descending).
*/
public void descendingSort()
{
// marker is the *array index* where left is all sorted
// and right is all non-sorted elements.
for (int marker = 1; marker < nElements; marker++)
{
long temp = array[marker];
// insert the marker into the left sorted elements
int insert;
for (insert = marker; insert > 0; insert--)
{
if (array[insert - 1] < temp)
{
array[insert] = array[insert - 1];
}
else
{
break;
}
}
array[insert] = temp;
}
}
}