How can I edit my code to make sure it prints out the efficient sorting algorithm for each txt file?

35 Views Asked by At

I wrote a program that tries to find out the most efficient sorting algorithm, starting from a very small unsorted txt file to a very large sorted txt file. I don't know why it is Insertion sort that is the answer for all of them. I am comparing it between Merge Sort, Bubble Sort and insertion Sort. I know my code isnt probably efficient as well..but yea.

  public static void printTests(String[] sortNames, int[] bubUp, int[] inserUp, int[] mergeUp) {
    String sortName = "";

    for (int i = 0; i < sortNames.length; i++) {
      sortName = sortNames[i];

      if (sortName.equals("BubbleSort")) {
        System.out.println("Bubble Sort Steps Taken:");
        for (int j = 0; j < bubUp.length; j++) {
          System.out.println("t" + (j + 1) + ": " + bubUp[j]);
        }
        System.out.println("\n");

      } else if (sortName.equals("InsertionSort")) {
        System.out.println("Insertion Sort Steps Taken:");
        for (int j = 0; j < inserUp.length; j++) {
          System.out.println("t" + (j + 1) + ": " + inserUp[j]);
        }

        System.out.println("\n");

      } else if (sortName.equals("MergeSort")) {
        System.out.println("Merge Sort Steps Taken:");
        for (int j = 0; j < mergeUp.length; j++) {
          System.out.println("t" + (j + 1) + ": " + mergeUp[j]);
        }
        System.out.println("\n");
      }
    }

    if (bubUp[0] < inserUp[0] && bubUp[0] < mergeUp[0]) {
      System.out.println(
          "The most efficient sorting algorithm for a very small dataset that is unordered (t1.txt) is Bubble Sort");
    } else if (inserUp[0] < bubUp[0] && inserUp[0] < mergeUp[0]) {
      System.out.println(
          "The most efficient sorting algorithm for a very small dataset that is unordered (t1.txt) is Insertion Sort");
    } else {
      System.out.println(
          "The most efficient sorting algorithm for a very small dataset that is unordered (t1.txt) is Merge Sort");
    }

    if (bubUp[1] < inserUp[1] && bubUp[1] < mergeUp[1]) {
      System.out.println(
          "The most efficient sorting algorithm for a very small dataset that is partially sorted (t2.txt) is Bubble Sort");
    } else if (inserUp[1] < bubUp[1] && inserUp[1] < mergeUp[1]) {
      System.out.println(
          "The most efficient sorting algorithm for a very small dataset that is partially sorted  (t2.txt) is Insertion Sort");
    } else {
      System.out.println(
          "The most efficient sorting algorithm for a very small dataset that is partially sorted  (t2.txt) is Merge Sort");
    }

    if (bubUp[2] < inserUp[2] && bubUp[2] < mergeUp[2]) {
      System.out.println(
          "The most efficient sorting algorithm for a small dataset that is unordered (t3.txt) is Bubble Sort");
    } else if (inserUp[2] < bubUp[2] && inserUp[2] < mergeUp[2]) {
      System.out.println(
          "The most efficient sorting algorithm for a small dataset that is unordered (t3.txt) is Insertion Sort");
    } else {
      System.out
          .println("The most efficient sorting algorithm for a small dataset that is unordered (t3.txt) is Merge Sort");
    }

    if (bubUp[3] < inserUp[3] && bubUp[3] < mergeUp[3]) {
      System.out.println(
          "The most efficient sorting algorithm for a small dataset that is partially sorted (t4.txt) is Bubble Sort");
    } else if (inserUp[3] < bubUp[3] && inserUp[3] < mergeUp[3]) {
      System.out.println(
          "The most efficient sorting algorithm for a small dataset that is partially sorted (t4.txt) is Insertion Sort");
    } else {
      System.out.println(
          "The most efficient sorting algorithm for a small dataset that is partially sorted (t4.txt) is Merge Sort");
    }

    if (bubUp[4] < inserUp[4] && bubUp[4] < mergeUp[4]) {
      System.out.println(
          "The most efficient sorting algorithm for a large dataset that is unordered (t5.txt) is Bubble Sort");
    } else if (inserUp[4] < bubUp[4] && inserUp[4] < mergeUp[4]) {
      System.out.println(
          "The most efficient sorting algorithm for a large dataset that is unordered (t5.txt) is Insertion Sort");
    } else {
      System.out
          .println("The most efficient sorting algorithm for a large dataset that is unordered (t5.txt) is Merge Sort");
    }

    if (bubUp[5] < inserUp[5] && bubUp[5] < mergeUp[5]) {
      System.out.println(
          "The most efficient sorting algorithm for a large dataset that is partially sorted (t6.txt) is Bubble Sort");
    } else if (inserUp[5] < bubUp[5] && inserUp[5] < mergeUp[5]) {
      System.out.println(
          "The most efficient sorting algorithm for a large dataset that is partially sorted (t6.txt) is Insertion Sort");
    } else {
      System.out.println(
          "The most efficient sorting algorithm for a large dataset that is partially sorted (t6.txt) is Merge Sort");
    }

    if (bubUp[6] < inserUp[6] && bubUp[6] < mergeUp[6]) {
      System.out.println(
          "The most efficient sorting algorithm for a very large dataset that is unordered (t7.txt) is Bubble Sort");
    } else if (inserUp[6] < bubUp[6] && inserUp[6] < mergeUp[6]) {
      System.out.println(
          "The most efficient sorting algorithm for a very large dataset that is unordered (t7.txt) is Insertion Sort");
    } else {
      System.out.println(
          "The most efficient sorting algorithm for a very large dataset that is unordered (t7.txt) is Merge Sort");
    }

    if (bubUp[7] < inserUp[7] && bubUp[7] < mergeUp[7]) {
      System.out.println(
          "The most efficient sorting algorithm for a very small dataset that is partially sorted  (t8.txt) is Bubble Sort");
    } else if (inserUp[7] < bubUp[7] && inserUp[7] < mergeUp[7]) {
      System.out.println(
          "The most efficient sorting algorithm for a very small dataset that is partially sorted  (t8.txt) is Insertion Sort");
    } else {
      System.out.println(
          "The most efficient sorting algorithm for a very small dataset that is partially sorted  (t8.txt) is Merge Sort");
    }

  }

I tried checking the counters for all the sorting algorithms and everything looks fine. but it still prints out this:

The most efficient sorting algorithm for a very small dataset that is unordered (t1.txt) is Insertion Sort
The most efficient sorting algorithm for a very small dataset that is partially sorted  (t2.txt) is Insertion Sort
The most efficient sorting algorithm for a small dataset that is unordered (t3.txt) is Insertion Sort
The most efficient sorting algorithm for a small dataset that is partially sorted (t4.txt) is Insertion Sort
The most efficient sorting algorithm for a large dataset that is unordered (t5.txt) is Insertion Sort
The most efficient sorting algorithm for a large dataset that is partially sorted (t6.txt) is Insertion Sort
The most efficient sorting algorithm for a very large dataset that is unordered (t7.txt) is Insertion Sort
The most efficient sorting algorithm for a very small dataset that is partially sorted  (t8.txt) is Insertion Sort
1

There are 1 best solutions below

0
Torben On

You did not provide the code for how you collected the data, so we can't tell if the bug is actually in that code. So I'm not going to bother to debug your code at all. I'm going to give some generic advice instead.

You approach the problem from the wrong angle. At any given time, you are only interested in the sorting results of a single file. Having the sorting results of the other files in the same primitive arrays only makes the problem hard to solve.

Storing the sorting results in primitive arrays where the indexes have a special meaning, makes the problem very hard to debug.

Get rid of the integer arrays and introduce a class named

public class SortingMeasurementResult {
    String sortingAlgorithm;
    int numOfComparisons;
}

Pick one file from your list of files. Run each sorting algorithm for that file only and generate an instance of SortingMeasurementResult to contain the results for that file and algorithm and store those objects to a List<SortingMeasurementResult >.

Sort that list using a comparator that compares the numOfComparisons field in ascending order. Pick the first one and print the value of sortingAlgorithm field.

Then go to the beginning and pick the next file.