Question: Using this JUnit test class: import static org.junit.Assert.*; /** * A test class that tests the methods of the MySorter class * to make sure
Using this JUnit test class:
import static org.junit.Assert.*;
/**
* A test class that tests the methods of the MySorter class
* to make sure they are using different sorting algorithms correctly.
*/
import org.junit.Test;
public class TestMySorter
{
/**
* Test Bubble Sort on a small, randomly ordered array
*/
@Test
public void testBubbleSmall()
{
int[] x = { 5, 3, 8, 1 };
MySorter s = new MySorter();
s.bubbleSort(x);
for (int i = 0; i < x.length - 1; i++)
{
assertTrue("Out of order at " + i, x[i] < x[i + 1]);
}
}
/**
* Test maxPosition for an element in the
* middle of the unsorted position
*/
@Test
public void testMaxPositionMiddle()
{
int[] x = {1, 5, 3, 7, 8, 9};
MySorter s = new MySorter();
assertEquals(1, s.maxPosition(x, 2));
}
/**
* Test maxPosition for an element in the middle
* of the unsorted portion
*/
@Test
public void testMaxPositionFirst()
{
int[] x = {5, 1, 3, 2, 8, 9};
MySorter s = new MySorter();
assertEquals(0, s.maxPosition(x, 3));
}
/**
* Test maxPosition for an element in the middle
* of the unsorted portion
*/
@Test
public void testMaxPositionLast()
{
int[] x = {1, 5, 3, 7, 8, 9};
MySorter s = new MySorter();
assertEquals(3, s.maxPosition(x, 3));
}
/**
* maxPosition must work correctly even when there is only
* one element in the portion of the array it is looking at
*/
@Test
public void testMaxPositionOneElement()
{
int[] x = {1, 5, 3, 7, 8, 9};
MySorter s = new MySorter();
assertEquals(0, s.maxPosition(x, 0));
}
/**
* Make sure that we can find the biggest thing
* even if it's negative
*/
@Test
public void testMaxPositionNegative()
{
int[] x = {-7, -6, -8,-3, -1};
MySorter s = new MySorter();
assertEquals(1, s.maxPosition(x, 2));
}
/**
* Test Selection Sort on a small, randomly ordered array
*/
@Test
public void testSelectionSmall()
{
int[] x = {5, 3, 8, 1};
MySorter s = new MySorter();
s.selectionSort(x);
for (int i = 0; i < x.length - 1; i++)
{
assertTrue("Out of order at " + i, x[i] < x[i + 1]);
}
}
/**
* Test insertion sort on a small, randomly ordered array
*/
@Test
public void testInsertionSmall()
{
int[] x = {5, 3, 8, 1};
MySorter s = new MySorter();
s.insertionSort(x);
for (int i = 0; i < x.length - 1; i++)
{
assertTrue("Out of order at " + i, x[i] < x[i + 1]);
}
}
}
And the class it tests:
import static org.junit.Assert.*;
/**
* A class that contains a number of different sorting algorithms.
*/
public class MySorter
{
/**
* Sort the given array
* @param array the array to be sorted
*/
public void bubbleSort(int[] array)
{
for (int i = 0; i { for (int j = 0; j < array.length - 1; j++) { if (array[j] > array[j + 1]) { swap(array, j, j+1); } } } } /** * Swap the values in two positions of an array * @param x the array * @param i the first position to be swapped * @param j the second position to be swapped */ private void swap(int[] x, int i, int j) { int t = x[i]; x[i] = x[j]; x[j] = t; } /** * Uses the given array to find the * index and location of the largest value * in the array * @param x the array * @param largestIndexOfUnsortedArray * @return the index at the max value */ public int maxPosition(int[] x, int largestIndexOfUnsortedArray) { int maxValueIndex = 0; int maxValue = x[maxValueIndex]; for (int i = 0; i <= largestIndexOfUnsortedArray; i++) { if (x[i] > maxValue) { maxValue = x[i]; maxValueIndex = i; } } return maxValueIndex; } /** * Sort the given array using selection sort * @param x the array to be sorted */ public void selectionSort(int[] x) { int temp, minInd; for(int i = 0; i < x.length; ++i) { minInd = i; for(int j = i; j < x.length; ++j) { if(x[j] < x[minInd]) { minInd = j; } } temp = x[minInd]; x[minInd] = x[i]; x[i] = temp; } } /** * Sort the given array using insertion sort * @param x the array to be sorted */ public void insertionSort(int[] x) { for (int i = 1; i < x.length; i++) { int temp = x[i]; int j = i - 1; while (j >= 0 && temp < x[j]) { x[j + 1] = x[j]; j = j - 1; } x[j+1] = temp; } } } Answer the following questions: a.) Explain why each of the for loops in Bubble Sort stop one index before the end of the array. Hint: the reasons aren't the same. b.) In the second description of Selection Sort (with the do-while loop), why was it OK that we stopped when the unsorted section still had one element in it? c.) Explain the four border cases we built for testing maxPosition().
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
