Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在一段时间后,如何停止排序算法(迭代和递归)?_Java_Sorting_Recursion_Time_Iteration - Fatal编程技术网

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

Java 在一段时间后,如何停止排序算法(迭代和递归)?,java,sorting,recursion,time,iteration,Java,Sorting,Recursion,Time,Iteration,现在,我的程序只是为快速排序、合并排序、冒泡排序、插入排序和选择排序的时间长度制作时间戳。我最终将编写代码,以便将信息存储在CSV文件中。但是,我希望排序在10分钟时停止(如果需要那么长的时间),并返回正好10分钟的等价值。关于我如何才能做到这一点,有什么想法吗 这是我到目前为止的所有代码 主要内容: package-pkgfinal; 导入java.io.File; 导入java.text.simpleDataFormat; 导入java.util.Calendar; 公开课决赛 { 公共静态

现在,我的程序只是为快速排序、合并排序、冒泡排序、插入排序和选择排序的时间长度制作时间戳。我最终将编写代码,以便将信息存储在CSV文件中。但是,我希望排序在10分钟时停止(如果需要那么长的时间),并返回正好10分钟的等价值。关于我如何才能做到这一点,有什么想法吗

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

主要内容:

package-pkgfinal;
导入java.io.File;
导入java.text.simpleDataFormat;
导入java.util.Calendar;
公开课决赛
{
公共静态void main(字符串[]args)
{
日历cal=空;
SimpleDataFormat sdf=新的SimpleDataFormat(“HH:mm:ss a”);
字符串[]sortName={“快速”、“合并”、“插入”、“选择”、“气泡”};
File toDir=新文件(“src\\Benchmark Data”);
提出基准;
final int NUM OF_SORTS=5;//正在测试的排序算法数
SortInfo[]sortInfoArray=新的SortInfo[NUM_OF_SORTS];
long[]result=新的long[NUM_OF_SORTS][];
int[]tempArr;//在排序之前填充随机整数
最终int[]临时数组大小={10000,20000,100000,2000000,1000000,2000000};
最终整数测试=5;
final int MAX_int=100000;//随机生成的数值上限
for(int i=0;i
我的快速排序:

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;
公共类快速排序
{
公共静态long[]getQuickSortResults(int[]a,int测试,int mi)
{
长[]时间=新长[a.长度];
对于(int k=0;k
我的合并排序:

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;
公共类合并排序
{
公共静态long[]getMergeSortResults(int[]a,int-tests,int-mi)
{
长[]时间=新长[测试];
对于(int k=0;kpackage 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;
    }
}