Ask Question
19 March, 01:19

Your application must generate: - an array of thirteen random integers from 1-99, - then prompt the user to select a sorting option (Bubble Sort, Insertion Sort, Shell Sort, Merge Sort, or Quick Sort)

+5
Answers (1)
  1. 19 March, 03:34
    0
    The code is given which can be pasted in the jа vascript 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]
    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;

    }

    }

    }
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Your application must generate: - an array of thirteen random integers from 1-99, - then prompt the user to select a sorting option (Bubble ...” in 📙 Engineering if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers