Question

In: Computer Science

in java. using the following template as a method, public static void shellSort(int[] array) { create...

in java. using the following template as a method,

public static void shellSort(int[] array) {

create a program that counts the number of comparisons and swaps of the sorting method does using

int array1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; // Best Case Test
int array2[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; // Worst Case Test
int array3[] = {10, 4, 8, 2, 6, 1, 9, 3, 7, 5}; // Average Case Test

and have the counsel print out the results.

Solutions

Expert Solution

public class Main
{
   public static void main(String[] args) {
       int array1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; //best case
       int array2[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; // Worst Case Test
        int array3[] = {10, 4, 8, 2, 6, 1, 9, 3, 7, 5};//average case
       int i,j,swaps1=0,comparisons1=0,len1,temp,swaps2=0,comparisons2=0,len2,len,swaps3=0,comparisons3=0,len3;
      
        //System.out.println("In Best case :\n1.swaps:"+swaps1+"2.comparisons :"+comparisons1);
        len1=array1.length;
        swaps1=0;
        temp=array1[0];
        for(i=0;i<len1;i++)
        {
            for(j=i+1;j<len1;j++)
            {comparisons1=comparisons1+1;
              
                if(array1[i]>array1[j])
                {
                    temp=array1[i];
                    array1[i]=array1[j];
                    array1[j]=temp;
                    swaps1=swaps1+1;
                }
            }
        }
        System.out.println("In Best case :\n1.swaps:"+swaps1+"\n2.comparisons :"+comparisons1);
        len2=array2.length;
        swaps2=0;
        for(i=0;i<len2;i++)
        {
            for(j=i+1;j<len2;j++)
            {comparisons2=comparisons2+1;
              
                if(array2[i]>array2[j])
                {
                    temp=array2[i];
                    array2[i]=array2[j];
                    array2[j]=temp;
                    swaps2=swaps2+1;
                }
            }
        }
        System.out.println("In Worst case :\n1.swaps:"+swaps2+"\n2.comparisons :"+comparisons2);
        len3=array3.length;
        swaps3=0;
        comparisons3=0;
        for(i=0;i<len3;i++)
        {
            for(j=i+1;j<len3;j++)
            {comparisons3=comparisons3+1;
              
                if(array3[i]>array3[j])
                {
                    temp=array3[i];
                    array3[i]=array3[j];
                    array3[j]=temp;
                    swaps3=swaps3+1;
                }
            }
        }
        System.out.println("In Average case :\n1.swaps:"+swaps3+"\n2.comparisons :"+comparisons3);
   }
}

output:

If you have any queries...please comment...Thank you...


Related Solutions

JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
Write a method public static void minMax(int[] arr) that takes an array of unique ints of...
Write a method public static void minMax(int[] arr) that takes an array of unique ints of length at least two as an argument, and swaps the smallest value of the array into the 0th position and swaps the largest value of the array into the last position. For example, if int[] a = {4, 3, 2, 6, 1, 5}, the method call minMax(a) should modify the array so that it is {1, 3, 2, 5, 4, 6}. The method should...
JAVA Given the header of a method public static void m1 (int[ ] max) Write down...
JAVA Given the header of a method public static void m1 (int[ ] max) Write down Java codes to invoke m1 method, declare variables as needed, (Do NOT implement the method)
rite a method with the following header: public static void showGradeDistribution(int a, int b, int c,...
rite a method with the following header: public static void showGradeDistribution(int a, int b, int c, int d, int f) It should print a graph (using asterisks) for each of the letters entered in the reverse order of the parameter list and with a label. In addition, if A and B grades sum is equal or exceeds that of grades C and D and F, the message “Strong class!” should be displayed. For example a method call of: showGradeDistribution(5,7,4,4,3); Would...
Write a method with the following header: public static void showGradeDistribution(int a, int b, int c,...
Write a method with the following header: public static void showGradeDistribution(int a, int b, int c, int d, int f) It should print a graph (using asterisks) for each of the letters entered in the reverse order of the parameter list and with a label. In addition, if A and B grades sum is equal or exceeds that of grades C and D and F, the message “Strong class!” should be displayed. For example a method call of: showGradeDistribution(5,7,4,4,3); Would...
Create a method:         Public Static boolean isSubArray489(int[] intArray) This method has an array of integers as its...
Create a method:         Public Static boolean isSubArray489(int[] intArray) This method has an array of integers as its input parameter, we'll say that a SubArray 9,8,7is that three integers, 9,8,7values appearing decreasing order one by one in the array. Return true if the array does contain any SubArray “987” Notice that any 3 integers that presenting a decreasing by one order must return True. (987, 876,765,654, 543, 432,321, 210 are all True) Call this method in your main function, and pass in...
class Main { public static void main(String[] args) {        int[] array = {1,2,3,4,5};   ...
class Main { public static void main(String[] args) {        int[] array = {1,2,3,4,5};        //Complexity Analysis //Instructions: Print the time complexity of method Q1_3 with respect to n=Size of input array. For example, if the complexity of the //algorithm is Big O nlogn, add the following code where specified: System.out.println("O(nlogn)"); //TODO }    public static void Q1_3(int[] array){ int count = 0; for(int i = 0; i < array.length; i++){ for(int j = i; j < array.length;...
Consider the following recursive method in Java public static int mystery(int n) {   if (n ==...
Consider the following recursive method in Java public static int mystery(int n) {   if (n == 0)   return 1;    else    return 4 * mystery (n - 1);   } What is the output of  mystery (3) using the code segment above Show your work on your trace file
public class Lab1 { public static void main(String[] args) { int array [] = {10, 20,...
public class Lab1 { public static void main(String[] args) { int array [] = {10, 20, 31, 40, 55, 60, 65525}; System.out.println(findPattern(array)); } private static int findPattern(int[] arr) { for (int i = 0; i < arr.length - 2; i++) { int sum = 0; for (int j = i; j < i + 2; j++) { sum += Math.abs(arr[j] - arr[j + 1]); } if (sum == 20) return i; } return -1; } } QUESTION: Modify the given...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT