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
1. Write a 400-500 word research report about Burke High School.
fenix001 [56]

Answer:

when is it due

Explanation:

6 0
3 years ago
WILL GIVE Brainliest!!!!
12345 [234]

Answer:

start

Explanation:

5 0
2 years ago
Read 2 more answers
True of False - use T or F An abstract class can have instances created using the constructor of the class.
Sindrei [870]

Answer:

False

Explanation:

An instance of an abstract class cannot be created through the constructor of the class, because it does not have a complete implementation. Though, it is possible to have references of an abstract type class. Abstract classes are incomplete structures and you would have to build onto it before you are able to use it.

7 0
4 years ago
Plz answer it’s timed
MAVERICK [17]

Answer: the third one

Explanation:

just trust me, the other ones dont mke sense to what I know about the subject, which is a lot

7 0
3 years ago
What is indentation?​
Vlad1618 [11]

Answer: may refer to: Indentation (typesetting), the placement of text farther to the right, or left, to separate it from surrounding text. Indentation style, in programming a convention governing the indentation of blocks of code to convey the program's structure.

Explanation:

5 0
3 years ago
Other questions:
  • Describe a scenario for which a find unmatched query could be used.
    12·1 answer
  • When long labels are required, which of these commands can you use to improve readability of a worksheet? .
    7·2 answers
  • Define a class named ComparableSquare that extends Square (defined above) and implements Comparable. Implement the compareTo met
    7·1 answer
  • Taking a picture with a wide aperture creates a shallow depth of field. The subject is up close and in focus and defined while b
    12·2 answers
  • Compare and Contrast - What is your opinion of the new VEX coding software.
    6·1 answer
  • Https://meetme.bit.nl/OfficialTrapstarz Join yall its my birthday
    13·2 answers
  • What will be the code in CSS, to set
    8·1 answer
  • After several rounds of sessions and reviews, you finally have a list of confirmed requirements from a client in the music indus
    8·1 answer
  • This is more a question for Brainly,
    5·2 answers
  • Write an inheritance hierarchy for classes Quadrilateral, Trapezoid, Parallelogram, Rectangle and Square. Use Quadrilateral as t
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!