如何在一定时间后停止排序算法(迭代和递归)?

时间:2018-04-16 18:29:20

标签: java sorting recursion time iteration

现在,我的程序只是为快速排序,合并排序,冒泡排序,插入排序和选择排序的时间长度排序时间戳。我最终将编写代码,以便将信息存储在CSV文件中。但是,我希望排序在10分钟时停止(如果需要那么长时间)并返回10分钟的等效值。关于我如何能够实现这一目标的任何想法?

到目前为止,这是我的所有代码。

主:

package pkgfinal;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class Final
{
    public static void main(String[] args)
    {
        Calendar cal = null;
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss a");
        String[] sortName = {"Quick", "Merge", "Insertion", "Selection", "Bubble"};

        File toDir = new File("src\\Benchmark Data");
        File outBenchmarks;

        final int NUM_OF_SORTS = 5; // number of sorting algorthims being tested

        SortInfo[] sortInfoArray = new SortInfo[NUM_OF_SORTS];
        long[][] result = new long[NUM_OF_SORTS][];
        int[] tempArr; // to fill with random ints before sorting
        final int[] TEMP_ARRAY_SIZE = {10000, 20000, 100000, 200000, 1000000, 2000000};

        final int TESTS = 5;
        final int MAX_INT = 100000; // randomly generated num cap

        for (int i = 0; i < sortInfoArray.length; i++) {
            sortInfoArray[i] = new SortInfo();
        }

        if (!(toDir.exists())) {
            toDir.mkdir();
        }

        outBenchmarks = new File(toDir, "FinalProjectBenchmarks.csv");

        for (int i = 0; i < TEMP_ARRAY_SIZE.length; i++) {
            tempArr = new int[TEMP_ARRAY_SIZE[i]];

            startStamp(cal, sdf, sortName[0], TEMP_ARRAY_SIZE[i]);
            result[0] = QuickSort.getQuickSortResults(tempArr, TESTS, MAX_INT);
            endStamp(cal, sdf, sortName[0]);

            startStamp(cal, sdf, sortName[1], TEMP_ARRAY_SIZE[i]);
            result[1] = MergeSort.getMergeSortResults(tempArr, TESTS, MAX_INT);
            endStamp(cal, sdf, sortName[1]);

            startStamp(cal, sdf, sortName[2], TEMP_ARRAY_SIZE[i]);
            result[2] = IterativeSorts.doInsertionSort(tempArr, TESTS, MAX_INT);
            endStamp(cal, sdf, sortName[2]);

            startStamp(cal, sdf, sortName[3], TEMP_ARRAY_SIZE[i]);
            result[3] = IterativeSorts.doSelectionSort(tempArr, TESTS, MAX_INT);
            endStamp(cal, sdf, sortName[3]);

            startStamp(cal, sdf, sortName[4], TEMP_ARRAY_SIZE[i]);
            result[4] = IterativeSorts.doBubbleSort(tempArr, TESTS, MAX_INT);
            endStamp(cal, sdf, sortName[4]);

            for (int j = 0; j < sortInfoArray.length; j++) {
                sortInfoArray[i].setAllNumResultInfo(result[i]); // total, best, worst, average times
                sortInfoArray[i].setArrSize(TEMP_ARRAY_SIZE[i]);
                sortInfoArray[i].setNumOfTests(TESTS);
            }
        }
    }

    public static void startStamp(Calendar cal, SimpleDateFormat sdf, String sort, int arrLength){
        cal = Calendar.getInstance();
        System.out.println(sdf.format(cal.getTime())+" - Starting "+sort+" - array length: "+arrLength);
    }
    public static void endStamp(Calendar cal, SimpleDateFormat sdf, String sort){
        cal = Calendar.getInstance();
        System.out.println(sdf.format(cal.getTime())+" - End "+sort+"\n");
    }

    public static void WritetoCSV(){

    }
}

我的快速排序:

package pkgfinal;

public class QuickSort
{
    public static long[] getQuickSortResults(int[] a, int tests, int mi)
    {
        long[] time = new long[a.length];

        for (int k = 0; k < tests; k++) {
            for (int i = 0; i < a.length; i++) {
                a[i] = (int)(Math.random()*mi +1);
            }

            long startTime = System.nanoTime();

            quickSort(a, 0, a.length - 1);

            long endTime = System.nanoTime();
            long duration = endTime - startTime;

            time[k] = duration;
        }

        return time;
    }

    public static void quickSort(int[] a, int startIndex, int endIndex)
    {
        int pivotIndex;

        if (startIndex < endIndex) {
            pivotIndex = partition(a, startIndex, endIndex);
            quickSort(a, startIndex, pivotIndex - 1);
            quickSort(a, pivotIndex + 1, endIndex);
        }
    }

    public static int partition(int[] a, int startIndex, int endIndex)
    {
        int pivotIndex;
        int pivotValue;
        int midIndex = startIndex;

        pivotIndex = (startIndex + endIndex) / 2;
        pivotValue = a[pivotIndex];

        swap(a, pivotIndex, endIndex);

        for (int i = startIndex; i < endIndex; i++) {
            if (a[i] < pivotValue) {

                swap(a, i, midIndex);
                midIndex = midIndex + 1;
            }
        }

        swap(a, midIndex, endIndex);

        return midIndex;
    }

    public static void swap(int[] a, int first, int second)
    {
        int temp;

        temp = a[first];
        a[first] = a[second];
        a[second] = temp;

    }
}

我的合并排序:

package pkgfinal;

public class MergeSort
{
    public static long[] getMergeSortResults (int[] a, int tests, int mi)
    {
        long[] time = new long[tests];

        for (int k = 0; k < tests; k++) {
            for (int i = 0; i < a.length; i++) {
                a[i] = (int)(Math.random()*mi +1);
            }
            int[] temp = new int[a.length];

            long startTime = System.nanoTime();

            splitArray(a, temp, 0, a.length - 1);

            long endTime = System.nanoTime();
            long duration = endTime - startTime;

            time[k] = duration;
        }

        return time;
    }

    public static void splitArray(int[] a, int[] temp, int low, int high)
    {
        int mid;

        if (high > low) {

            mid = (low+high)/2;
            splitArray(a, temp, low, mid );
            splitArray(a, temp, mid+1, high);

            mergeArrays(a, temp, low, mid, high);
        }

        return;
    }

    public static void mergeArrays(int[] a, int[] temp, int low, int mid, int high)
    {
        for (int i = low; i <= high; i++) {
            temp[i] = a[i];
        }

        int lowP = low;
        int highP = mid + 1;
        int aP = low;

        while ((lowP <= mid) && (highP <= high)) {

            if (temp[lowP] <= temp[highP]) {
                a[aP] = temp[lowP];
                lowP++;
            } else {
                a[aP] = temp[highP];
                highP++;
            }

            aP++;
        }

        if (lowP > mid) 
            for (int i = highP; i <= high; i++) {
                a[aP] = temp[i];
                aP++;
            }
        else
            for (int i = lowP; i <= mid; i++) {
                a[aP] = temp[i];
                aP++;
            }

        return;
    }
}

迭代排序:

package pkgfinal;

public class IterativeSorts
{
    public static long[] doInsertionSort(int[] a, int tests, int mi)
    {
        long time[] = new long[tests];
        for (int t = 0; t < tests; t++)
        {
            for (int f = 0; f < a.length; f++) {
                a[f] = (int)(Math.random()*mi +1);
            }

            int temp;

            long startTime = System.nanoTime();

            for (int i = 1; i < a.length; 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;
                    }
                }
            }
            long endTime = System.nanoTime();

            long elapsedTime = endTime - startTime;

            time[t] = elapsedTime;
        }

        return time;
    }

    public static long[] doSelectionSort(int[] a, int tests, int mi)
    {
        long time[] = new long[tests];
        for (int t = 0; t < tests; t++) {
            for (int f = 0; f < a.length; f++) {
                a[f] = (int)(Math.random()*mi +1);
            }

            long startTime = System.nanoTime();

            for (int i = 0; i < a.length - 1; i++)
            {
                int index = i;
                for (int j = i + 1; j < a.length; j++)
                    if (a[j] < a[index]) 
                        index = j;

                int smallerNumber = a[index];  
                a[index] = a[i];
                a[i] = smallerNumber;
            }

            long endTime = System.nanoTime();

            long elapsedTime = endTime - startTime;

            time[t] = elapsedTime;
        }

        return time;
    }

    public static long[] doBubbleSort (int[] a, int tests, int mi)
    {
        long[] time = new long[tests];
        int temp;
        int n = a.length;
        int k;

        for (int t = 0; t < tests; t++) {
            for (int f = 0; f < a.length; f++) {
                a[f] = (int)(Math.random()*mi +1);
            }

            long startTime = System.nanoTime();

            for (int m = n; m >= 0; m--) {
                for (int i = 0; i < n - 1; i++) {
                    k = i + 1;
                    if (a[i] > a[k]) {
                        temp = a[i];
                        a[i] = a[k];
                        a[k] = temp;
                    }
                }
            }

            long endTime = System.nanoTime();

            long elapsedTime = endTime - startTime;

            time[t] = elapsedTime;
        }

        return time;
    }
}

0 个答案:

没有答案