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
iren [92.7K]
3 years ago
11

Your application must generate: - an array of thirteen random integers from 1-99, - then prompt the user to select a sorting opt

ion (Bubble Sort, Insertion Sort, Shell Sort, Merge Sort, or Quick Sort)
Engineering
1 answer:
Fynjy0 [20]3 years ago
5 0

Answer:

The code is given which can be pasted in the Javascript file

Explanation:

The code is given as below

package Sorters;

public class javasort

{

private int[] arr=new int[13];

public void bubbleSort(int[] a){

int c,d,temp;

for (c = 0; c < ( 13 - 1 ); c++) {

for (d = 0; d < 13- c - 1; d++) {

if (a[d] > a[d+1]) /* For descending order use < */

{

temp = a[d];

a[d] = a[d+1];

a[d+1] = temp;

}

}

 

System.out.print("\n[");

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

  System.out.print(a[i]+" ");

}

System.out.print("]");

}

System.out.println("\nSorted list of numbers");

System.out.print("[");

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

  System.out.print(a[i]+" ");

}

System.out.print("]");

}

public void insertionSort(int[] a){

int temp;

for (int i = 1; i < 13; i++) {

for(int j = i ; j > 0 ; j--){

if(a[j] < a[j-1]){

temp = a[j];

a[j] = a[j-1];

a[j-1] = temp;

}

}

System.out.print("\n[");

for (int c = 0; c < 13; c++){

  System.out.print(a[c]+" ");

}

System.out.print("]");

}

System.out.println("\nSorted list of numbers");

System.out.print("[");

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

  System.out.print(a[i]+" ");

}

System.out.print("]");

}

public void shellSort(int[] a){

int increment = a.length / 2;

while (increment > 0)

{

for (int i = increment; i < a.length; i++)

{

int j = i;

int temp = a[i];

while (j >= increment && a[j - increment] > temp)

{

a[j] = a[j - increment];

j = j - increment;

}

a[j] = temp;

}

if (increment == 2)

increment = 1;

else

increment *= (5.0 / 11);

System.out.print("\n[");

for (int c = 0; c < 13; c++){

  System.out.print(a[c]+" ");

}

System.out.print("]");

}

System.out.println("\nSorted list of numbers");

System.out.print("[");

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

  System.out.print(a[i]+" ");

}

System.out.print("]");

}

public void MergeSort(int[] a, int low, int high){

int N = high - low;  

if (N <= 1)

return;

int mid = low + N/2;

// recursively sort

MergeSort(a, low, mid);

MergeSort(a, mid, high);

// merge two sorted subarrays

int[] temp = new int[N];

int i = low, j = mid;

for (int k = 0; k < N; k++)

{

if (i == mid)

temp[k] = a[j++];

else if (j == high)

temp[k] = a[i++];

else if (a[j]<a[i])

temp[k] = a[j++];

else

temp[k] = a[i++];

}

for (int k = 0; k < N; k++)

a[low + k] = temp[k];

System.out.print("\n[");

for (int c = 0; c < 13; c++){

  System.out.print(a[c]+" ");

}

System.out.print("]");

printM(a);

}

public void quickSort(int[] a,int low,int high){

  System.out.print("\n[");

  for (int c = 0; c < 13; c++){

      System.out.print(a[c]+" ");

  }

  System.out.print("]");

int i =low, j = high;

int temp;

int pivot = a[(low + high) / 2];

/** partition **/

while (i <= j)

{

while (a[i] < pivot)

i++;

while (a[j] > pivot)

j--;

if (i <= j)

{

/** swap **/

temp = a[i];

a[i] = a[j];

a[j] = temp;

i++;

j--;

}

}

/** recursively sort lower half **/

if (low < j)

quickSort(a, low, j);

/** recursively sort upper half **/

if (i < high)

quickSort(a, i, high);

printM(a);

}

public void printM(int[] a){

arr=a;

}

public void fPrint(){

  System.out.println("\nSorted list:");

  System.out.print("\n[");

  for (int c = 0; c < 13; c++){

      System.out.print(arr[c]+" ");

  }

  System.out.print("]");

}

}

package mani;

import java.util.Random;

import java.util.Scanner;

public class javasorttest

{

 

public static void main(String[] args){

int[] a=new int[13];

Random r=new Random();

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

a[i]=r.nextInt(99)+1;

}

System.out.print("[");

for (int c = 0; c < 13; c++){

  System.out.print(a[c]+" ");

}

System.out.print("]");

javasort j=new javasort();

System.out.println("\nSelect the sorting algo.\n1.bubbleSort\n2.insertionSort\n3.shellSort\n4.MergeSort\n5.QuickSort.");

Scanner s=new Scanner(System.in);

int opt=s.nextInt();

switch(opt){

case 1:

j.bubbleSort(a);

break;

case 2:

j.insertionSort(a);

 

break;

case 3:

j.shellSort(a);

break;

case 4:

j.MergeSort(a, 0, 13);

j.fPrint();

break;

case 5:

j.quickSort(a ,0, 12);

j.fPrint();

break;

}

}

}

You might be interested in
2. Describe the three phases of the life cycle curve Draw the curve and label it in detail (the
miskamm [114]
57665554455555555rrtttyyyyuuiyt
5 0
3 years ago
Carbon dioxide (CO2) is compressed in a piston-cylinder assembly from p1 = 0.7 bar, T1 = 320 K to p2 = 11 bar. The initial volum
tekilochka [14]

Answer:

W_{12}=-53.9056KJ

Part A:

Q=-7.03734 KJ/Kg (-ve sign shows heat is getting out)

Part B:

Q=1.5265KJ/Kg (Heat getting in)

The value of Q at constant specific heat is approximately 361% in difference with variable specific heat and at constant specific heat Q has opposite direction (going in) than Q which is calculated in Part B from table A-23. So taking constant specific heat is not a good idea and is questionable.

Explanation:

Assumptions:

  1. Gas is ideal
  2. System is closed system.
  3. K.E and P.E is neglected
  4. Process is polytropic

Since Process is polytropic so  W_{12} =\frac{P_{2}V_{2}-P_{1}V_{1}}{1-n}

Where n=1.25

Since Process is polytropic :

\frac{V_{2}}{V_{1}}=(\frac{P_{1}}{P_{2}})^{\frac{1}{1.25}} \\V_{2}= (\frac{P_{1}}{P_{2}})^{\frac{1}{1.25}} *V_{1}

V_{2}= (\frac{0.7}{11})^{\frac{1}{1.25}} *0.262\\V_{2}=0.028924 m^3

Now,W_{12} =\frac{P_{2}V_{2}-P_{1}V_{1}}{1-n}

W_{12} =\frac{11*0.028924-0.7*0.262}{1-1.25}(\frac{10^{5}N/m^2}{1 bar})(\frac{1  KJ}{10^{3}Nm})

W_{12}=-53.9056KJ

We will now calculate mass (m) and Temperature T_2.

m=\frac{P_{1}V_{1}}{RT_{1}}\\ m=\frac{0.7*0.262}{\frac{8.314KJ}{44.01Kg.K}*320}(\frac{10^{5}N/m^2}{1 bar})(\frac{1  KJ}{10^{3}Nm})\\m=0.30338Kg

T_{2} =\frac{P_{2}V_{2}}{Rm}\\ m=\frac{11*0.028924}{\frac{8.314KJ}{44.01Kg.K}*0.30338}(\frac{10^{5}N/m^2}{1 bar})(\frac{1  KJ}{10^{3}Nm})\\T_{2} =555.14K

Part A:

According to energy balance::

Q=mc_{v}(T_{2}-T_{1})+W_{12}

From A-20, C_v for Carbon dioxide at 300 K is 0.657 KJ/Kg.k

Q=0.30338*0.657(555.14-320)+(-53.9056)

Q=-7.03734 KJ/Kg (-ve sign shows heat is getting out)

Part B:

From Table A-23:

u_{1} at 320K = 7526 KJ/Kg

u_{2} at 555.14K = 15567.292 (By interpolation)

Q=m(\frac{u(T_{2})-u(T_{1})}{M} )+W_{12}

Q=0.30338(\frac{15567.292-7526}{44.01} )+(-53.9056)

Q=1.5265KJ/Kg (Heat getting in)

The value of Q at constant specific heat is approximately 361% in difference with variable specific heat and at constant specific heat Q has opposite direction (going in) than Q which is calculated in Part B from table A-23. So taking constant specific heat is not a good idea and is questionable.

7 0
4 years ago
What is the importance of knowing the parts and function of the ignition system in servicing your own vehicle
lisov135 [29]

Answer:

El conocer la función del sistema nervioso permite comprender la estructura del cuerpo. Puesto que, toda acción que se se realice en ella dependerá del sistema nervioso para que sea eficaz

4 0
2 years ago
I have a keen eye and I compose all of the shots for a scene
JulijaS [17]

Answer:

script supervisor

Explanation:

8 0
3 years ago
What is "Engineering"?
Liono4ka [1.6K]
Engineering is when both the application of science and math are used to solve problems. An engineer is a person who designs, builds, or maintains engines, machines, or public works. There are six large branches of engineering such as Mechanical, Chemical, Civil, Electrical, Management, and Geotechnical, they all have hundreds of subcategories of engineering under each different branch.
5 0
3 years ago
Other questions:
  • A tiny, glass sphere has a charge of +10.9 nC. A metal ball bearing, which is 4.00 cm above the glass sphere, experiences an ele
    5·1 answer
  • Which of the following does NOT describe product design.
    11·1 answer
  • The free convection heat transfer coefficient on a thin hot vertical plate suspended in still air can be determined from observa
    9·1 answer
  • What are 5 tactics that can reduce the likelihood of injury?
    6·1 answer
  • Put four red LED as a straight line and connect each of them to a corresponding analog output. Connect a potentiometer to a 5 Vo
    12·1 answer
  • List two common units of measurement to describe height
    5·2 answers
  • Five batch jobs A through E arrive at a computer center in the order A to E at almost the same time. They have estimated running
    11·1 answer
  • 2. A
    12·1 answer
  • To measure the greening of the U.S. economy, you need only to look at the growing number of green jobs and occupations.
    10·2 answers
  • What is the number model for three families live in the same apartment
    8·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!