Question: JUNIT Write unit tests for one of the sorting algorithms below (use either: Shotgun Sort, QuickSort Algorithm or BubbleSort) * Write 3 black box tests*

JUNIT

Write unit tests for one of the sorting algorithms below (use either: Shotgun Sort, QuickSort Algorithm or BubbleSort) *Write 3 black box tests* from any of the following: Non functional technique or Boundary value analysis or Equivalence partition technique. or Error Guess

Only test the Sort Method.

Below are the 3 Sorting Algorithms

Shotgun Sort

import java.util.*;

public class ShotgunSort

{

// This sorting method is also known as BogoSort. It is not efficient (not

// even a little).

public static void sort(int[] arr)

{

while(!isSorted(arr))

shuffle(arr);

}

private static boolean isSorted(int[] arr)

{

for(int i = 0 ; i < arr.length - 1; i++)

if (arr[i] > arr[i + 1])

return false;

return true;

}

private static void shuffle(int[] arr)

{

Random rand = new Random();

for(int i = arr.length - 1; i > 0; i--)

{

int index = rand.nextInt(i+1);

int tmp = arr[index];

arr[index] = arr[i];

arr[i] = tmp;

}

}

public static void main(String args[])

{

// Transform args into int array. This is a demo, so if it crashes,

// that's on you.

int[] arr = new int[args.length];

for(int i = 0; i < args.length; i++)

arr[i] = Integer.parseInt(args[i]);

sort(arr);

for(int i : arr)

System.out.println(i);

}

}

QuickSort Algorithm

public class QuickSort

{

// From http://www.geeksforgeeks.org/quick-sort/

/* This function takes last element as pivot,

places the pivot element at its correct

position in sorted array, and places all

smaller (smaller than pivot) to left of

pivot and all greater elements to right

of pivot */

private static int partition(int arr[], int low, int high)

{

int pivot = arr[high];

int i = (low-1); // index of smaller element

for (int j=low; j

{

// If current element is smaller than or

// equal to pivot

if (arr[j] <= pivot)

{

i++;

// swap arr[i] and arr[j]

int temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

}

}

// swap arr[i+1] and arr[high] (or pivot)

int temp = arr[i+1];

arr[i+1] = arr[high];

arr[high] = temp;

return i+1;

}

/* The main function that implements QuickSort()

arr[] --> Array to be sorted,

low --> Starting index,

high --> Ending index */

private static void sort(int arr[], int low, int high)

{

if (low < high)

{

/* pi is partitioning index, arr[pi] is

now at right place */

int pi = partition(arr, low, high);

// Recursively sort elements before

// partition and after partition

sort(arr, low, pi-1);

sort(arr, pi+1, high);

}

}

public static void sort(int arr[])

{

sort(arr, 0, arr.length - 1);

}

public static void main(String args[])

{

// Transform args into int array. This is a demo, so if it crashes,

// that's on you.

int[] arr = new int[args.length];

for(int i = 0; i < args.length; i++)

arr[i] = Integer.parseInt(args[i]);

sort(arr);

for(int i : arr)

System.out.println(i);

}

}

BubbleSort

public class BubbleSort {

// From: https://www.javatpoint.com/bubble-sort-in-java

public static void sort(int[] arr) {

int n = arr.length;

int temp = 0;

for(int i=0; i < n; i++){

for(int j=1; j < (n-i); j++){

if(arr[j-1] > arr[j]){

//swap elements

temp = arr[j-1];

arr[j-1] = arr[j];

arr[j] = temp;

}

}

}

}

public static void main(String args[]) {

// Transform args into int array. This is a demo, so if it crashes,

// that's on you.

int[] arr = new int[args.length];

for(int i = 0; i < args.length; i++)

arr[i] = Integer.parseInt(args[i]);

sort(arr);

for(int i : arr)

System.out.println(i);

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!