如何编辑我的代码以确保它打印出每个txt文件的有效排序算法?

问题描述 投票:0回答:1

我写了一个程序,试图找出最有效的排序算法,从一个非常小的未排序的txt文件开始到一个非常大的排序的txt文件。我不知道为什么插入排序是所有问题的答案。我正在比较归并排序、冒泡排序和插入排序。我知道我的代码可能也效率不高..但是是的.

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

  }

我尝试检查所有排序算法的计数器,一切看起来都很好。但它仍然打印出这个:

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
java sorting mergesort bubble-sort insertion-sort
1个回答
0
投票

您从错误的角度处理问题。在任何给定时间,您只对单个文件的排序结果感兴趣。将其他文件的排序结果放在相同的原始数组中只会使问题难以解决。

将排序结果存储在原始数组中,其中索引具有特殊含义,使得问题很难调试。

摆脱整数数组并引入一个名为

的类
public class SortingMeasurementResult {
    String sortingAlgorithm;
    int numOfComparisons;
}

从文件列表中选择一个文件。仅针对该文件运行每个排序算法,并生成 SortingMeasurementResult 的实例以包含该文件和算法的结果,并将这些对象存储到

List<SortingMeasurementResult >

使用比较器对该列表进行排序,该比较器按升序比较

numOfComparisons
字段。选择第一个并打印
sortingAlgorithm
字段的值。

然后转到开头并选择下一个文件。

© www.soinside.com 2019 - 2024. All rights reserved.