/**
* Name:
*
* Date:
*/
public class ArrayUtil
{
/**
*Precondition: length > 0
*Postconditon: Creates an array filled with 'length'
* number of random values between 1 and n.
*@param length: the length of the array
*@param n: the number of possible random values
*@return an array filled with 'length' numbers between 1 and n.
*/
public static int[] randomIntArray(int length, int n)
{
int[] ans = new int[length];
for (int i = 0; i < length; i++)
{
ans[i] = (int) (Math.random() * n + 1);
}
return ans;
}
/**
*Precondition: 0 <= start <= end < nums.length
*Postcondition: Returns a String of the values of
*the array from start to end inclusive.
*Print 15 values per line with a newline after all values
*@param start: the first position to print
*@param end: the last position to print.
*@return a String of the values in nums from start to end.
*/
public static String printArray(int start, int end, int[] nums)
{
String ans = "";
for (int i = start; i <= end; i++)
{
if (i % 15 == 0 && i != 0)
ans += "\n";
ans += nums[i] + " ";
}
return ans;
}
/**
*Precondition: 0 <= from < nums.length
*Postcondition: Finds the smallest element in nums in the
*tail range of the array.
*@param from: the first position in 'nums' to compare
*@return the position of the smallest element in the
*range 'from' to 'nums.length-1'.
*/
public static int minimumPosition(int from, int[] nums)
{
int minp = from;
for (int i = from + 1; i < nums.length; i++)
{
if (nums[i] < nums[minp])
minp = i;
}
return minp;
}
/**
*Precondition: 0 <= i < a.length, 0 <= j < a.length
*Postcondition: Swaps two entries of the array.
*@param i: the first position to swap
*@param j: the second position to swap
*@return a with indexes i and j swapped
*/
public static int[] swap(int i, int j, int[] a)
{
int hold = a[i];
a[i] = a[j];
a[j] = hold;
return a;
}
/*Precondition: 0 <= start <= end < nums.length
Postcondition: shift all values in nums right one
from start to end
*@return nums with all values from start to
* to end shifted one index higher. keep all other
* values in nums the same
*/
public static int[] shiftRight(int start, int end, int[] nums)
{
for (int i = end; i > start; i--)
{
nums[i] = nums[i-1];
}
return nums;
}
/**
*Precondition: a.length > 0 && a is sorted in ascending order
*Postcondition: Returns the index where insertNum could be placed
*to keep the array a in ascending sorted order.
*@param insertNum: the number to insert into a.
*@return: An index of a. insertNum could be inserted @return to keep
*a in ascending sorted order.
*/
public static int findIndexToInsert(int insertNum, int[] a)
{
for (int i = 0; i < a.length; i++)
{
if (insertNum < a[i])
return i;
}
return -1;
}
/**
*Precondition: a.length > 0 && a is sorted in ascending order
*Postcondition: Insert insertNum into the sorted array into the
*correct position to keep a sorted in ascending order. This will
*increase the length of the a array by 1. All elements greater
*than insertNum will move to index + 1. Hint: You will need to create
*a new array one bigger than a. Copy all the old values of a
*into the new array with inserting insertNum.
*You must include calls to findIndexToInsert.
*@param insertNum: the number to insert into a.
*@return the array in ascending order with insertNum
*/
public static int[] insertSorted(int insertNum, int[] a)
{
int[] b = new int[a.length + 1];
b = a;
int index = findIndexToInsert(insertNum, a);
shiftRight(index, b.length - 1, b);
b[index] = insertNum;
return b;
}