In: Computer Science
If you have any problem with the code feel free to comment.
Program
class ManagedArray {
private int[] managedIntegerArray; // this is the array that we are managing
private int maximumSize; // this will hold the size of the array
private int currentSize = 0; // this will keep track of what positions in the array have been used
private final int DEFAULT_SIZE = 10; // the default size of the array
public ManagedArray()// default constructor initializes array to DEFAULT_SIZE
{
managedIntegerArray = new int[DEFAULT_SIZE];
maximumSize = DEFAULT_SIZE;
}
public ManagedArray(int size)// parameterized constructor initializes array to specified size
{
managedIntegerArray = new int[size];
maximumSize = size;
}
public ManagedArray(int[] arrayToCopy)// copy constructor
{
managedIntegerArray = new int[arrayToCopy.length];
maximumSize = arrayToCopy.length;
currentSize = maximumSize;
for (int i = 0; i < arrayToCopy.length; i++) {
managedIntegerArray[i] = arrayToCopy[i];
}
}
public void add(int integerToAdd)// adds a new integer to the array
{
if (currentSize < maximumSize)// if the new integer fits in the existing array, add it
{
managedIntegerArray[currentSize] = integerToAdd;
currentSize++;
} else// if the new integer does not fit in the existing array, make a new array, copy
// the old one into it, and add the new integer to the new array
{
expand();
managedIntegerArray[currentSize] = integerToAdd;
currentSize++;
}
}
public void remove(int index)// remove an item from the array and shift all the values over
{
if (index < maximumSize - 1) {
for (int i = index; i < maximumSize - 1; i++) {
managedIntegerArray[i] = managedIntegerArray[i + 1];
}
currentSize--;
} else if (index == maximumSize - 1) {
currentSize--;
}
}
public void bubbleSort() {
// code for bubble sort goes here
for (int i = currentSize - 1; i > 1; i--) {// ascending order
for (int j = 0; j < i; j++) {
if (managedIntegerArray[j] > managedIntegerArray[j + 1]) {
swap(j, j + 1);
}
}
}
}
public void selectionSort() {
// code for selection sort goes here
for (int i = 0; i < currentSize; i++) {
int min = i;
for (int j = i; j < currentSize; j++) {
if (managedIntegerArray[min] > managedIntegerArray[j])
min = j;
}
swap(min, i);
}
}
public void swap(int index1, int index2) {
// code for swap goes here
int temp = managedIntegerArray[index1];
managedIntegerArray[index1] = managedIntegerArray[index2];
managedIntegerArray[index2] = temp;
}
public int findMaxIndex(int limit) {
// code to find the INDEX of the maximum value in the array goes here
int max = managedIntegerArray[0];
int index = 0;
for (int i = 1; i < currentSize; i++) {
if (max < managedIntegerArray[i]) {
max = managedIntegerArray[i];
index = i;
}
}
return index;
}
private void expand()// create a new array and copy the existing one into it
{
int[] tempArray = new int[maximumSize * 2];
int i = 0;
for (int x : managedIntegerArray) {
tempArray[i++] = x;
}
managedIntegerArray = tempArray;
maximumSize = managedIntegerArray.length;
}
@Override // display the array
public String toString() {
String arrayString = "";
for (int i = 0; i < currentSize; i++) {
arrayString += (managedIntegerArray[i] + "\n");
}
return arrayString;
}
}
public class Test {//test class
public static void main(String[] args) {
int[] ar1 = { 6, 5, 8, 4, 3, 2, 8 };
ManagedArray ma1 = new ManagedArray(ar1);
int[] ar2 = { 10, 63, 48, 12, 32, 74 };
ManagedArray ma2 = new ManagedArray(ar2);
// sorting ma1 using bubble sort
ma1.bubbleSort();
System.out.println("Managed Array 1: \n" + ma1);
// sorting ma2 using selection sort
ma2.selectionSort();
System.out.println("Managed Array 2: \n" + ma2);
}
}
Output