0

I cant find an effective way to format the inside of the array to one decimal place.

Then there is a issue with the stopwatch I have towards the end of the code to keep track of how long it takes for the bubble sort to process based on the array size.

import java.util.Arrays;
import java.util.Random;

public class BubbleSortTest {
    public static void sort(double arr[]) {
        int arrayLength = arr.length;
        for (int i = 0; i < arrayLength-1; i++) {
            for (int j = 0; j < arrayLength-i-1; j++) {
                if (arr[j] > arr[j+1]) {
                    //swap temp and arr[i]
                    double temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
        }
    }

/* Prints the array */
public static void printArray(double arr[]) {
    int arrayLength = arr.length;
    for (int i = 0; i < arrayLength; ++i) {
        System.out.print(arr[i] + " ");
    }
    System.out.println();
}

public static void generateDoubleArray(double arr[], int length) {
    //generating random values
    Random rand = new Random();
    for(int i = 0; i < length; i++) {
        arr[i] = (rand.nextDouble() * (100 - 0)) + 0;
    }

}

public static void main(String args[]) {

    BubbleSortTest ob = new BubbleSortTest();
    int length = 3; //change the length according to requirement
    double arr[] = new double[length]; // Ex) {6.1, 4.2, 3.3, 7.4, 5.5, 2.6, 8.7, 1.8};
    BubbleSortTest.generateDoubleArray(arr, length);
    System.out.println("BEFORE BUBBLE SORT: "+ Arrays.toString(arr));
    BubbleSortTest.sort(arr);
    System.out.println("AFTER BUBBLE SORT: "+ Arrays.toString(arr));

    StopWatch s = new StopWatch();
    double[] a;

    for(int i = 1; i <= 10; i++) {
        a = BubbleSortTest.generateDoubleArray(i * 20000, i*30000);
        s.start();
        sort(a);
        s.stop();
        System.out.println("Size: " + i*1000 + "\t\tTime: " +s.elapsedTime());
    }
}
   // Size: 1000        Time: 0.0
   // Size: 2000        Time: 0.0
   // Size: 3000        Time: 0.0
   // Size: 4000        Time: 0.0
   // Size: 5000        Time: 0.0
   // Size: 6000        Time: 0.0
   // Size: 7000        Time: 0.0
   // Size: 8000        Time: 0.0
   // Size: 9000        Time: 0.0
   // Size: 10000       Time: 0.0

 // Output with length of array = 3 
 // BEFORE BUBBLE SORT: [56.24793454034215, 1.3614871074902335,17.853054450932547]
  //AFTER BUBBLE SORT: [1.3614871074902335,17.853054450932547,56.24793454034215]
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245

1 Answers1

0

So, you're not calling your printArray method...

There isn't a way to format doubles within a number array. You'll have to create a new String array with the formatted values, then print that, if you want to use Arrays.toString

If you use your own method, you can see several solutions at Best way to Format a Double value to 2 Decimal places

Regarding the error, it's not clear what you're expecting generateDoubleArray(i * 20000, i*30000); to do, but the first parameter is required to be a double array, not a number, plus the method itself returns nothing, so you won't be able to do a = BubbleSortTest.generateDoubleArray

Perhaps you wanted something like so

static Random rand = new Random();

public static double[] generateDoubleArray(int length){
    double arr[] = new double[length];
    for(int i = 0; i<length;i++) {
        //generating random values
        arr[i] = rand.nextDouble() * 100;
    }
    return arr;
}

Besides that, ("Size: " + i*1000 + in the output is way far off from the length parameter that you're trying to create

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245