1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
just olya [345]
3 years ago
14

Create four methods named insertionSort(), bubblesort(), mergesort() and quicksort(). Implement the sorting algorithms in the co

rresponding methods. In the main method, create four arrays a, b, c, d and initialize them with the same values. Pass these four arrays in four different functions. Create four instance variables named insertionSwap, bubbleSwap, countMerge, and countQuickSort and initialize them to 0. In methods insertionSort(), bubblesort() and quicksort(), increment the variables insertionSwap, bubbleSwap, and countQuickSort respectively by one whenever they call the method swap(). In method mergesort(), increment the variable countMerge by one to count the swaps. Use method swap() to swap the values.
Computers and Technology
1 answer:
Maru [420]3 years ago
4 0

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

 

You might be interested in
What was one important academic skill the blogger learned?
denis23 [38]

An academic skill that the blogger learned, was to review the graded tests and quizzes to see which concepts he missed out on so that in the future he would be well prepared for it. Reviewing and comparing the tests and quizzes would give him an idea on which topics he needs to study.

Other than the academic skill, the blogger also learned that a person should never give up and he keeps on learning throughout his entire life, and that all of the improvements that the blogger made did not come all at once.

4 0
4 years ago
You have a chart that shows 100 data points and you've circled the highest value. Which of the following are you using?
cricket20 [7]

The Rudolph Rule states that  simple ways you can make information stand out and guide or satisfy your audience to important details and highlight important  information in your presentation

so i conclude option D is correct for above statement

hope it helps

5 0
3 years ago
Read 2 more answers
A ____ error occurs when the javascript interpreter encounters a problem while a program is executing.
Leya [2.2K]
It is a runtime error thart occurs when <span>the javascript interpreter encounters a problem while a program is executing.</span>

5 0
3 years ago
Write a program whose input is two integers. Output the first integer and subsequent increments of 10 as long as the value is le
Lubov Fominskaja [6]

Answer:

import java.util.Scanner;

public class TestClock {

   public static void main(String[] args) {

       Scanner in = new Scanner(System.in);

       System.out.println("Enter two integer numbers");

       int num1 = in.nextInt();

       int num2 = in.nextInt();

       int newSum=num1+10;

       System.out.println("The first number is "+num1);

       do{

          System.out.println(newSum);

          newSum +=10;

       }while (newSum <=num2);

   }

}

Explanation:

Using Java Programming language

  1. Prompt user for the two inputs and save them as num1 and num2(Using the scanner class)
  2. Create a new Variable newSum = num1+10
  3. Create a do...while loop to continually print the value of newSum, and increment it by 10 while it is less or equal to num2
3 0
3 years ago
Draw a Hierarchical input process output ( HIPO ) chart to represent a high - level view of the functions of the proposed system
Morgarella [4.7K]

The format to use in drawing the Hierarchical input process output  chart to show a high - level view of the functions of the proposed system is given in the image attached.

<h3>What is hierarchical input process output?</h3>

An HIPO model is known to be a form of hierarchical input process output model that helps in  systems analysis design and also in documentation .

Note that it is often used for depicting the modules of a system based on the use of hierarchy and for saving each module and thus by following the method used in the image attached, one can draw a Hierarchical input process output ( HIPO ) chart to represent a high - level view of the functions of the proposed system.​

Learn more about HIPO  from

brainly.com/question/2665138

#SPJ1

5 0
2 years ago
Other questions:
  • AYUDAAA..... DARÉ TODOS LOS PUNTOS QUE PUEDA.
    15·1 answer
  • True or false: Sony is the company behind the creation of the ‘Super Mario Bros.' franchise.
    8·2 answers
  • Create a Rational number class in Java using the same style as the Complex number class created in class.(The in class example c
    12·1 answer
  • The partners of a small architectural firm are constantly busy with evolving client requirements. To meet the needs of their cli
    11·1 answer
  • How do you do basic addition and subtraction in binary, octal and hex?
    12·1 answer
  • What is an internal node?
    11·1 answer
  • What can search the internet and select elements based on important words
    10·1 answer
  • You can access various sites on WWW by using hyperlinks or by?
    6·2 answers
  • 1st answer will get brainliest!
    13·2 answers
  • PLEASE SOMEONE ANSWER THIS
    8·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!