You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改代码以正确输出各TXT数据集对应的高效排序算法?

问题

我编写了一个程序,用于对比Merge Sort、Bubble Sort和Insertion Sort三种算法在从极小无序到极大有序的TXT数据集上的效率,但所有测试结果都显示Insertion Sort是最优解,这不符合预期。我检查了各排序算法的计数器,看似正常,但输出结果始终全部为Insertion Sort。

以下是我的printTests方法代码:

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\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\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\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

请问如何修改代码,以确保能正确输出每个TXT文件对应的高效排序算法?

解决方案

1. 先验证计数器的准确性

首先要确认bubUp、inserUp、mergeUp三个数组的数值是否真实反映了各算法的执行步骤。可以在判断最优算法前,针对每个数据集打印三个算法的具体步骤数,方便排查:

// 在原有代码的判断逻辑前添加
for (int i = 0; i < bubUp.length; i++) {
    System.out.printf("数据集t%d.txt: Bubble=%d, Insertion=%d, Merge=%d%n", 
                      i+1, bubUp[i], inserUp[i], mergeUp[i]);
}

如果发现Merge Sort的步骤数始终远高于Insertion,问题大概率出在排序算法的计数器实现上:

  • 归并排序的递归调用中没有累加计数器(比如只统计了主函数的步骤,遗漏了递归分支的操作)
  • 三种算法的统计标准不一致(比如Insertion统计比较次数,Merge统计的是比较+移动的总次数,导致数值偏高)
  • Merge Sort的计数器未在每次测试前重置,导致数值累加

2. 修复比较逻辑的漏洞

当前代码的else分支直接默认归并排序最优,但未处理多个算法步骤数相同的情况,同时可以重构重复代码,减少冗余:

新增工具方法判断最优算法

private static String getBestAlgorithm(int bubbleSteps, int insertionSteps, int mergeSteps) {
    int minSteps = Math.min(Math.min(bubbleSteps, insertionSteps), mergeSteps);
    if (bubbleSteps == minSteps) {
        return "Bubble Sort";
    } else if (insertionSteps == minSteps) {
        return "Insertion Sort";
    } else {
        return "Merge Sort";
    }
}

重构printTests的判断逻辑

把原来重复的8段判断代码替换为循环处理,同时保留调试输出:

// 定义数据集描述,避免重复字符串
String[] datasetInfos = {
    "a very small dataset that is unordered (t1.txt)",
    "a very small dataset that is partially sorted (t2.txt)",
    "a small dataset that is unordered (t3.txt)",
    "a small dataset that is partially sorted (t4.txt)",
    "a large dataset that is unordered (t5.txt)",
    "a large dataset that is partially sorted (t6.txt)",
    "a very large dataset that is unordered (t7.txt)",
    "a very small dataset that is partially sorted (t8.txt)"
};

// 循环处理每个数据集
for (int i = 0; i < datasetInfos.length; i++) {
    // 打印调试信息
    System.out.printf("[调试] %s: Bubble=%d, Insertion=%d, Merge=%d%n", 
                      datasetInfos[i], bubUp[i], inserUp[i], mergeUp[i]);
    // 获取最优算法
    String bestSort = getBestAlgorithm(bubUp[i], inserUp[i], mergeUp[i]);
    // 输出结果
    System.out.printf("The most efficient sorting algorithm for %s is %s%n%n", 
                      datasetInfos[i], bestSort);
}

3. 排查归并排序的计数器实现

如果调试后确认Merge Sort的步骤数异常,检查归并排序的代码:

  • 确保递归调用时计数器是传递引用或静态变量正确累加(比如用类成员变量作为计数器,每次测试前重置为0)
  • 统一统计标准:三种算法都统计元素比较次数,或都统计元素移动/交换次数,不能混合统计
  • 检查归并排序的merge操作中是否漏统计了步骤(比如合并两个子数组时的比较和移动都要计入)

内容的提问来源于stack exchange,提问作者Debby Fo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 20:57:32