Answer:
Step 1
The JAVA program uses four different methods named insertionSort(), bubblesort(), mergesort() and quicksort() to implement four different algorithms identical to the name of each method to sort four identical integer arrays then print the number of swaps made by each algorithm to sort the array.
Approach: -
Creating the main class and declaring four instance variables insertionSwap, bubbleSwap, countMerge, and countQuickSort and initializing them with zero. These variables are used to store the number of swaps made in each technique to sort the array.
Defining the main method, and creating four integer arrays a[], b[], c[] and d[] and initializing them with same integer values.
Inside the main() calling the four methods insertionSort(), bubblesort(), mergesort() and quicksort(). These methods are defined outside the main method.
Defining the methods insertionSort(), bubblesort(), mergesort() and quicksort() one by one using the four instance variables insertionSwap, bubbleSwap, countMerge, and countQuickSort to count the number of swaps made in each technique to sort the four identical arrays.
In the above-defined method, four sorting algorithms are implemented identically to their names.
The swap() method is defined to swap the values.
Step 2
Java Program: -
//main class
public class Main
{
//declaring the four instance variables
static int insertionSwap = 0; //initializing with zero
static int bubbleSwap = 0; //initializing with zero
static int countMerge=0; //initializing with zero
static int countQuickSort=0; //initializing with zero
//main method
public static void main(String[] args)
{
//declaring the four arrays
//initializing them with same values
//array a[]
int[] a = {2,34,56,21,46,31,77,28,67,68,22,81,91,61,89,33,44,99,18,109};
//array b[]
int[] b = {2,34,56,21,46,31,77,28,67,68,22,81,91,61,89,33,44,99,18,109};
//array c[]
int[] c = {2,34,56,21,46,31,77,28,67,68,22,81,91,61,89,33,44,99,18,109};
//array d[]
int[] d = {2,34,56,21,46,31,77,28,67,68,22,81,91,61,89,33,44,99,18,109};
//calling the functions in the main method
//passing a as the parameter
insertionSort(a);
//passing b as the parameter
bubbleSort(b);
//passing c as the parameter
mergeSort(c);
//passing d as the parameter
quickSort(d);
//displaying the message
//displaying the swap count made in insertionSort method
System.out.println("Number of swaps in insertionSort is: "+insertionSwap);
//displaying the swap count made in bubbleSort method
System.out.println("Number of swaps in bubbleSort is: "+bubbleSwap);
//displaying the swap count made in mergeSort method
System.out.println("Number of swaps in mergeSort is: "+countMerge);
//displaying the swap count made in quickSort method
System.out.println("Number of swaps in quickSort is: "+countQuickSort);
}
//defining the method insertionSort and passing an integer array
public static int[] insertionSort(int[] ar)
{
//first for-loop is used
for (int p = 1; p < ar.length; p++)
{
//inserting the elements of array
int val = ar[p];
//declaring the variable
int q;
//second for loop
for (q = p-1; q >= 0 && ar[q] > val; q--)
{
//incrementing the count
insertionSwap++;
//swapping the values
ar[q +1] = ar[q];
}
//swapping the values
ar[q +1] = val;
}
//return the array
return ar;
}
//defining the method bubbleSort and passing an integer array
public static int[] bubbleSort(int[] ar)
{
//storing the length of array in the variable n
int n = ar.length;
//first for loop
for (int p = 0; p < n-1; p++)
//second for loop
for (int q = 0; q < n-p-1; q++)
//to check whether the element stored at index q is greater than the element at q+1
if (ar[q] > ar[q+1])
{
//calling the swap function
swap(ar,q,q+1);
//incrementing the count
bubbleSwap++;
}
//return the array
return ar;
}
//defining the method bubbleSort and passing an integer array
public static int[] mergeSort(int[] ar)
{
//return statement
return mergeSort(ar, 0, ar.length-1);
}
//defining the method bubbleSort and passing an integer array and two integer variables