简体   繁体   English

将数组传递给排序方法以查找其执行时间

[英]Passing arrays to the sorting methods to find their execution time

I'm assigned this project where I need to find execution time for different arrays. 我分配了这个项目,我需要在其中找到不同数组的执行时间。 My code is working completely fine except for the part when my array moves from selection sort to bubble and from bubble to insertion and to merge. 我的代码工作正常,除了我的数组从选择排序移至冒泡,从冒泡移至插入和合并的那部分。 The problem is when my array which random number between 1 - 1000 is getting passed to selection sort and it sorts it out and that sorted array is getting passed to rest of the sorting method. 问题是当我的数组(其中1-1000之间的随机数)传递给选择排序并对其进行排序,而该排序后的数组传递给其余的排序方法时。 I tried going through my code again but I can't find what the problem is. 我尝试再次查看代码,但找不到问题所在。 Am I supposed to populate an array or something? 我应该填充数组吗?

Any help is appreciated 任何帮助表示赞赏

package sorting;

import java.util.Arrays;
import java.util.Random;
import java.lang.Math;
import java.io.*;
import java.util.*;

public class sorting {

public static void main(String[] args) {

    double[] arr1 = getArray(10000);
    double[] arr2 = getArray(20000);
    double[] arr3 = getArray(40000);
    double[] arr4 = getArray(80000);

    System.out.println("Problem 1: Selection sort, Bubble sort, Insertion sort, and Merge sort with thier execution time");
    System.out.println("");

    System.out.printf("%12s%15s%15s%17s%13s\n", "Number of Elements |", "Selection Sort", "Bubble Sort", "Insertion Sort", "Merge Sort");
    System.out.println("--------------------------------------------------------------------------------");

    long b = System.currentTimeMillis();
    long startTime = System.currentTimeMillis();

    selectionSort(arr1);
    long endTime = System.currentTimeMillis();
    long executionTime = endTime - startTime;
    System.out.printf("%11s", arr1.length);
    System.out.printf("%18s%2s", executionTime, " ms");


    startTime = System.currentTimeMillis();
    bubbleSort(arr1);
    endTime = System.currentTimeMillis();
    executionTime = endTime - startTime;
    System.out.printf("%12s%2s", executionTime, " ms");

    startTime = System.currentTimeMillis();
    insertionSort(arr1);
    endTime = System.currentTimeMillis();
    executionTime = endTime - startTime;
    System.out.printf("%12s%2s", executionTime, " ms");

    startTime = System.currentTimeMillis();
    (new MergeSort()).mergeSort(arr1);
    endTime = System.currentTimeMillis();
    executionTime = endTime - startTime;
    System.out.printf("%12s%2s\n", executionTime, " ms");

    System.out.printf("%11s", arr2.length);
    startTime = System.currentTimeMillis();
    selectionSort(arr2);
    endTime = System.currentTimeMillis();
    executionTime = endTime - startTime;
    System.out.printf("%18s%2s", executionTime, " ms");

    startTime = System.currentTimeMillis();
    bubbleSort(arr2);
    endTime = System.currentTimeMillis();
    executionTime = endTime - startTime;
    System.out.printf("%12s%2s", executionTime, " ms");

    startTime = System.currentTimeMillis();
    insertionSort(arr2);
    endTime = System.currentTimeMillis();
    executionTime = endTime - startTime;
    System.out.printf("%12s%2s", executionTime, " ms");

    startTime = System.currentTimeMillis();
    (new MergeSort()).mergeSort(arr2);
    endTime = System.currentTimeMillis();
    executionTime = endTime - startTime;
    System.out.printf("%12s%2s\n", executionTime, " ms");

    System.out.printf("%11s", arr3.length);
    startTime = System.currentTimeMillis();
    bubbleSort(arr3);
    endTime = System.currentTimeMillis();
    executionTime = endTime - startTime;
    System.out.printf("%18s%2s", executionTime, " ms");

    startTime = System.currentTimeMillis();
    bubbleSort(arr3);
    endTime = System.currentTimeMillis();
    executionTime = endTime - startTime;
    System.out.printf("%12s%2s", executionTime, " ms");

    startTime = System.currentTimeMillis();
    insertionSort(arr3);
    endTime = System.currentTimeMillis();
    executionTime = endTime - startTime;
    System.out.printf("%12s%2s", executionTime, " ms");

    startTime = System.currentTimeMillis();
    (new MergeSort()).mergeSort(arr3);
    endTime = System.currentTimeMillis();
    executionTime = endTime - startTime;
    System.out.printf("%12s%2s\n", executionTime, " ms");

    System.out.printf("%11s", arr4.length);
    startTime = System.currentTimeMillis();
    bubbleSort(arr4);
    endTime = System.currentTimeMillis();
    executionTime = endTime - startTime;
    System.out.printf("%18s%2s", executionTime, " ms");

    startTime = System.currentTimeMillis();
    bubbleSort(arr4);
    endTime = System.currentTimeMillis();
    executionTime = endTime - startTime;
    System.out.printf("%12s%2s", executionTime, " ms");

    startTime = System.currentTimeMillis();
    insertionSort(arr4);
    endTime = System.currentTimeMillis();
    executionTime = endTime - startTime;
    System.out.printf("%12s%2s", executionTime, " ms");

    startTime = System.currentTimeMillis();
    (new MergeSort()).mergeSort(arr4);
    endTime = System.currentTimeMillis();
    executionTime = endTime - startTime;
    System.out.printf("%12s%2s\n", executionTime, " ms");


}

 public static double[] getArray(int n){
    double[] arr = new double[n];
    for(int i=0; i<n; i++){
        arr[i] = ((double)(Math.random()*1000));
    }
    return arr;
}


public static void selectionSort(double[] arr) {
    for (int i = 0; i < arr.length - 1; i++) {

        double min = arr[i];
        int currentMin = i;
        for (int j = i + 1; j < arr.length; j++) {
            if (min > arr[j]) {
                min = arr[j];
                currentMin = j;
            }
        }
        if (currentMin != i) {
            arr[currentMin] = arr[i];
            arr[i] = min;

        }
    }

}

public static double[] insertionSort(double[] arr) {
    double temp;
    for (int i = 1; i < arr.length; i++) {

        for (int j = i; j > 0; j--) {
            if (arr[j] < arr[j - 1]) {
                temp = arr[j];
                arr[j] = arr[j - 1];
                arr[j - 1] = temp;
            }
        }
    }
    return arr;
}

public static double[] bubbleSort(double[] arr) {
    double temp;
    for (int i = 0; i < arr.length; i++) {
        // bubble up
        for (int j = 0; j < arr.length - 1 - i; j++) {
            if (arr[j] > arr[j + 1]) {
                temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;

            }
        }
    }
    return arr;
}

public static class MergeSort {

    private double[] array;
    private double[] temp;
    private int length;

    public double[] mergeSort(double[] arr) {
        this.array = arr;
        this.length = arr.length;
        this.temp = new double[length];
        merge1(0, length - 1);
        return arr;
    }

    private void merge1(int left, double right) {

        if (left < right) {
            double middle = left + (right - left) / 2;
            merge1(left, middle);
            merge1((int) (middle + 1), right);
            merge2(left, middle, right);
        }
    }

    private void merge2(int left, double middle, double right) {

        for (int i = left; i <= right; i++) {
            temp[i] = array[i];
        }
        int i = left;
        int j = (int) (middle + 1);
        int k = left;
        while (i <= middle && j <= right) {
            if (temp[i] <= temp[j]) {
                array[k] = temp[i];
                i++;
            } else {
                array[k] = temp[j];
                j++;
            }
            k++;
        }
        while (i <= middle) {
            array[k] = temp[i];
            k++;
            i++;
        }

    }
}
}

This is what I'm getting output as Problem 1: Selection sort, Bubble sort, Insertion sort, and Merge sort with thier execution time 这就是我作为问题1输出的内容:选择排序,气泡排序,插入排序和合并排序以及执行时间

Number of Elements | Selection Sort    Bubble Sort   Insertion Sort   Merge 
Sort
----------------------------------------------------------------------------

  10000                85 ms          31 ms          31 ms          16 ms
  20000               385 ms         100 ms         123 ms          10 ms
  40000              3085 ms         401 ms         347 ms           8 ms
  80000             13231 ms        1972 ms        1484 ms          35 ms

The problem is when my array which random number between 1 - 1000 is getting passed to selection sort and it sorts it out and that sorted array is getting passed to rest of the sorting method 问题是当我的数组(其中1-1000之间的随机数)传递给选择排序并对其进行排序,而已排序的数组传递给其余排序方法时

You shall find out the root of the cause at Is Java "pass-by-reference" or "pass-by-value"? 您应在Java是“按引用传递”还是“按值传递”中找到原因的根源

For you, this is because you are passing the value of the array arr1 when you call 对您来说,这是因为您在调用时传递了数组arr1的值

selectionSort(arr1); 

The manipulations done to arr1 in this case updates the arr1 . 在这种情况下对arr1进行的操作会更新arr1 The next time you pass this to the 下次您将此传递给

bubbleSort(arr1); // this uses the updated array.

You can update your code to use the randomly generated array from your existing method : 您可以更新代码以使用现有方法中随机生成的数组:

selectionSort(getArray(10000)); // similarily for other cases

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM