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
fredd [130]
3 years ago
14

Implement a method called bubbleSort, that takes an ArrayList, sorts it using bubble sort algorithm, and returns a sorted list;

2. Implement a method called selectionSort, that takes an ArrayList, sorts it using selection sort algorithm, and returns a sorted list; 3. Implement a method called insertionSort, that takes an ArrayList, sorts it using insertion sort algorithm, and returns a sorted list; 4. Implement a method called mergeSort, that takes an ArrayList, sorts it using merge sort algorithm, and returns a sorted list.
Computers and Technology
1 answer:
boyakko [2]3 years ago
6 0

Answer:

Java algorithm is given below

Explanation:

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Random;

public class Temp {

   static void bubbleSort(ArrayList<Integer> list) {

       int n = list.size();

       for (int p = 0; p < n - 1; p++)

           for (int q = 0; q < n - p - 1; q++)

               if (list.get(q) > list.get(q + 1)) {

                   int temp = list.get(q);

                   list.set(q, list.get(q + 1));

                   list.set(q + 1, temp);

               }

   }

   static void selectionSort(ArrayList<Integer> list) {

       int n = list.size();

       for (int p = 0; p < n - 1; p++) {

           int minimumIndex = p;

           for (int q = p + 1; q < n; q++)

               if (list.get(q) < list.get(minimumIndex))

                   minimumIndex = q;

           int temp = list.get(p);

           list.set(p, list.get(minimumIndex));

           list.set(minimumIndex, temp);

       }

   }

   static void insertionSort(ArrayList<Integer> list) {

       int size = list.size();

       int p, val, q;

       for (p = 1; p < size; p++) {

           val = list.get(p);

           q = p - 1;

           while (q >= 0 && list.get(q) > val) {

               list.set(q + 1, list.get(q));

               q = q - 1;

           }

           list.set(q + 1, val);

       }

   }

   static void mergeSort(ArrayList<Integer> list, int low, int high) {

       if (low < high && (high - low) >= 1) {

           int mid = (high + low) / 2;

           mergeSort(list, low, mid);

           mergeSort(list, mid + 1, high);

           merger(list, low, mid, high);

       }

   }

   static void merger(ArrayList<Integer> list, int low, int mid, int high) {

       ArrayList<Integer> mergedArray = new ArrayList<Integer>();

       int left = low;

       int right = mid + 1;

       while (left <= mid && right <= high) {

           if (list.get(left) <= list.get(right)) {

               mergedArray.add(list.get(left));

               left++;

           } else {

               mergedArray.add(list.get(right));

               right++;

           }

       }

       while (left <= mid) {

           mergedArray.add(list.get(left));

           left++;

       }

       while (right <= high) {

           mergedArray.add(list.get(right));

           right++;

       }

       int i = 0;

       int j = low;

       while (i < mergedArray.size()) {

           list.set(j, mergedArray.get(i++));

           j++;

       }

   }

   public static void main(String[] args) throws Exception {

       ArrayList<Integer> list = new ArrayList<>();

       Random rand = new Random(System.currentTimeMillis());

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

           list.add(rand.nextInt(256));

       }

       long start = System.currentTimeMillis();

       selectionSort(list);

       int sel = (int) (System.currentTimeMillis() - start);

       System.out.println("Selection Sort time (in ms): " + sel);

       list.clear();

       // ------------------------

       rand = new Random(System.currentTimeMillis());

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

           list.add(rand.nextInt(256));

       }

       start = System.currentTimeMillis();

       insertionSort(list);

       int ins = (int) (System.currentTimeMillis() - start);

       System.out.println("Insertion Sort time (in ms): " + ins);

       list.clear();

       // ------------------------

       rand = new Random(System.currentTimeMillis());

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

           list.add(rand.nextInt(256));

       }

       start = System.currentTimeMillis();

       bubbleSort(list);

       int bub = (int) (System.currentTimeMillis() - start);

       System.out.println("Bubble Sort time (in ms): " + bub);

       list.clear();

       // ---------------------------

       rand = new Random(System.currentTimeMillis());

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

           list.add(rand.nextInt(256));

       }

       start = System.currentTimeMillis();

       mergeSort(list, 0, list.size() - 1);

       int mer = (int) (System.currentTimeMillis() - start);

       System.out.println("Merge Sort time (in ms): " + mer);

       long m = Math.min(Math.min(sel, ins), Math.min(bub, mer));

       if (m == sel)

           System.out.println("Selection Sort is fastest");

       else if (m == ins)

           System.out.println("insertion Sort is fastest");

       else if (mer == m)

           System.out.println("Merge Sort is fastest");

       else if (m == bub)

           System.out.println("Bubble Sort is fastest");

   }

}

You might be interested in
Why do we need to send emails
DiKsa [7]

Answer:

email is the easier way of communicating and fast

5 0
4 years ago
how to write a function "void funct()" which will accept a string from the user as input and will then display the string backwa
Mariulka [41]

Answer:

#include <bits/stdc++.h>

using namespace std;

void funct(){

   string name;

   cout<<"enter the string: ";

   cin>>name;

   

    reverse(name.begin(), name.end());

    cout<<"The string is : "<<name<<endl;

   

}

int main()

{

   funct();

 

  return 0;

}

Explanation:

create the function funct() with return type void and declare the variable type string and print a message for asking to used enter the string.  

The string enter by user is store in the variable using cin instruction.

after that, we use a inbuilt function reverse() which takes two argument.

firs argument tell the starting point and second index tell the ending point. then, the reverse function reverse the string.

name.begin() it is a function which return the pointer of first character of string.

name.end()  it is a function which return the pointer of last character of the string.

finally, print the reverse string.

for calling the function, we have to create the main function and then call the function.

5 0
3 years ago
Mary works for a multimedia firm, and she spends long hours on a desktop computer. Recently, she expired back pain and visited h
Sindrei [870]

To stand as she works.

5 0
3 years ago
In computer science what are the methods used to protect the information stored by a piece of software called?
Nezavi [6.7K]

Answer:

cryptography

Explanation:

cryptography

In computer science, cryptography refers to secure information and communication techniques derived from mathematical concepts and a set of rule-based calculations called algorithms, to transform messages in ways that are hard to decipher

3 0
3 years ago
On Windows computers, applications from Microsoft will not look and act the same as windows and menus from the operating system.
Andrej [43]

True.

Windows is an operating system built by Microsoft. Chrome and Android are operating systems built by Google. MacOS (Macbook pro etc) and iOS (iPhone, iPad, Apple Watch etc) are operating systems built by Apple.

Engineers build applications or apps for all of the above operating systems (OS). For example, Microsoft Word menus will look slightly different on Windows vs on a Mac. The underlying functionality maybe the same but the way it looks may have subtle differences.

Think of the OS as the foundation of a building. Once you have the foundation, you can build a house, a skyscraper, an office building etc depending on what you want to build.

6 0
3 years ago
Read 2 more answers
Other questions:
  • Populate a one-dimensional array with the following grades in this order: 90, 61, 74, 42, 83, 51, 71, 83, 98, 87, 94, 68, 44, an
    15·1 answer
  • Use of the internet to access programs and data on computers that’s are owned and managed by the other using large data centers.
    11·1 answer
  • What is the name of the program file that you can enter in the Windows search or Run box to execute Event Viewer? What process i
    12·1 answer
  • To create a chart, you need to select at least
    8·1 answer
  • A basic program to find the area of a square​
    6·1 answer
  • Explain different types of networking-based attacks
    5·1 answer
  • This code --&gt; plt.plot(x,y) is used to draw :
    8·1 answer
  • Means storing,accessing information over the internet other than hard drive and your system
    15·1 answer
  • LAB: Phone number breakdown Given a long long integer representing a 10-digit phone number, output the area code, prefix, and li
    8·1 answer
  • Data will zip through a 5G network much faster than through a 4G network because of the 10-fold difference in _____.
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!