Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write an application that allows you to initialize and use a one-dimensional arr

ID: 3837015 • Letter: W

Question

Write an application that allows you to initialize and use a one-dimensional array of 15 double numbers.. Create and call the method which initializes the array with random numbers generated by the Java Random class. Create and use the custom method to sort the array values in a descending orderThen create and use the custom method to sort the array in an ascending order. Do not use any of the prebuilt sorting methods of the Array class— any sort type must be implemented by you from scratch, using e.g., bubble sort algorithm , binary sort algorithm or others. Write a separate displayArray method that takes the sorted array as an argument and displays the array element values, first time you will use this method to display descending order array values, the second time you will use this method to display ascending order array values. Make your application user friendly and explain to potential users what they see ( array of randomly generated values, sorted array values in descending order, sorted array values in ascending order).

Explanation / Answer

HI, Please find my implementation.

Please let me know in case of any issue.

import java.util.Random;

public class SortDoubleArray {

  

   // bubble sort

   public static void bubbleSortAescending(double array[]) {

int n = array.length;

int k;

for (int m = n; m >= 0; m--) {

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

k = i + 1;

if (array[i] > array[k]) {

double temp = array[i];

array[i] = array[k];

array[k] = temp;

}

}

  

}

}

  

   // bubble sort

       public static void bubbleSortDescending(double array[]) {

      int n = array.length;

      int k;

      for (int m = n; m >= 0; m--) {

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

      k = i + 1;

      if (array[i] < array[k]) {

      double temp = array[i];

      array[i] = array[k];

      array[k] = temp;

      }

      }

  

      }

      }

  

   private static void displayArray(double[] input) {

  

for (int i = 0; i < input.length; i++) {

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

}

System.out.println();

}

   public static void main(String[] args) {

      

      

       int n = 15;

      

       // creating an double arrray of size n

       double arr[] = new double[n];

      

       // Random number Object

       Random random = new Random();

      

       // filling array with random numbers in range 1-100

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

           arr[i] = random.nextDouble()*100; // in range 1-100

       }

      

       System.out.println("original Array");

       // calling prdouble method

       displayArray(arr);

      

       // calling bubble sort methods

       bubbleSortAescending(arr);

      

       System.out.println("Ascending Sorted Array");

       // calling print method

       displayArray(arr);

      

       bubbleSortDescending(arr);

      

       System.out.println("Descending Sorted Array");

       displayArray(arr);

      

   }

}

/*

Sample run:

original Array

32.037618126261016 24.870505760486537 29.86528083954305 28.146115789983874 99.55342120599163 73.21252170081335 23.82466345492995 16.000066377300048 45.393862781014846 38.028173877051316 29.690048128982248 93.91384900236429 98.26365470884923 82.07633083978135 1.793584015854599

Ascending Sorted Array

1.793584015854599 16.000066377300048 23.82466345492995 24.870505760486537 28.146115789983874 29.690048128982248 29.86528083954305 32.037618126261016 38.028173877051316 45.393862781014846 73.21252170081335 82.07633083978135 93.91384900236429 98.26365470884923 99.55342120599163

Descending Sorted Array

99.55342120599163 98.26365470884923 93.91384900236429 82.07633083978135 73.21252170081335 45.393862781014846 38.028173877051316 32.037618126261016 29.86528083954305 29.690048128982248 28.146115789983874 24.870505760486537 23.82466345492995 16.000066377300048 1.793584015854599

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote