0

Right now, my program simply makes time stamps for sorting time lengths of Quick Sort, Merge Sort, Bubble Sort, Insertion Sort, and Selection Sort. I eventually will be writing the code so the info is stored in a CSV file. However, I want the sorts to stop at 10 minutes (if and when it takes that long at all) and return the equivalent value of exactly 10min. Any ideas on how I may be able to accomplish this?

Here's all my code so far.

Main:

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(){

    }
}

My Quick Sort:

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;

    }
}

my Merge Sort:

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;
    }
}

Iterative Sorts:

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;
    }
}
Jazz4Dayz
  • 1
  • 2
  • for the recursive functions, you could pass in the start time to the functions, checking in the begginning of the method if the current time - the passed in time is greater than some constant value like 5000ms, return right away. – RAZ_Muh_Taz Apr 16 '18 at 18:43
  • Possible duplicate of [How to stop execution after a certain time in Java?](https://stackoverflow.com/questions/4252187/how-to-stop-execution-after-a-certain-time-in-java) – William Perron Apr 16 '18 at 19:17
  • @WilliamPerron I did look at that post but I was having a hard time seeing how I could implement threads into my code. – Jazz4Dayz Apr 16 '18 at 19:32
  • @Jazz4Dayz the question linked has nothing to do with threads, also, you should explain in your question why that question did not solve your problem if you don't want you question to be closed as a duplicate – William Perron Apr 16 '18 at 19:34

0 Answers0