【问题标题】:How to use recursion to sort an array of strings from smallest to largest?如何使用递归对字符串数组从小到大进行排序?
【发布时间】:2016-10-31 04:04:42
【问题描述】:
import java.io.File;
import java.util.Scanner;

public class TestDriver {

public static void main(String[] args) {
    Scanner x = null;
    try {
        x = new Scanner(new File("pokemon"));
    } catch (Exception e) {
        System.out.println("could not find file");
    }

    @SuppressWarnings("resource")
    Scanner input = new Scanner(System.in);
    System.out.println("Type in the number of Pokemons (1-15)!");
    int userNumber = 0;
    boolean userFalse = false;
    while (!userFalse) { // Validates user inputs for years
        if (input.hasNextInt()) {
            int temp = input.nextInt();
            if (temp < 1 || temp > 15) { // Years cannot be below 0
                System.out.println("Invalid input.");
                userFalse = false;
            } else {
                userFalse = true;
                userNumber = temp;
            }
        } else {
            System.out.println("Try Again!");
            input.next();
        }
    }
    String[] a = new String[userNumber];
    for (int i = 0; i < userNumber; i++) {
        a[i] = x.next();
        System.out.print(a[i] + " ");
    }
    sort(a, userNumber);
}

在 pokemon.txt 中,它读取

Gyarados  
Lapras 
Eevee  
Vaporeon 
Snorlax
Abra
Slowbro
Rhyson
Kyogre
Blastoise
Jigglypuff
Miltank
Lugia
Steelix
Arbok

我正在尝试将口袋妖怪名称从小到大排序。我不知道做到这一点的最佳方法。我的老师希望我使用递归来做到这一点。这与快速排序或合并排序相同吗?提前致谢。 编辑:这是我尝试使用 mergesort 进行排序:

public static void sort(String[] pokemon, int userNumber) {

    String[] a = new String[pokemon.length / 2]; // Split array into two
    String[] b = new String[pokemon.length - a.length]; // halves, a and b
    for (int i = 0; i < pokemon.length; i++) {
        if (i < a.length)
            a[i] = a[i];
        else
            b[i - a.length] = pokemon[i];
    }

    sort(a, userNumber); // Recursively sort first
    sort(b, userNumber); // and second half.

    int ai = 0; // Merge halves: ai, bi
    int bi = 0; // track position in
    while (ai + bi < pokemon.length) { // in each half.
        if (bi >= b.length || (ai < a.length && a[ai].length() < b[bi].length())) {
            pokemon[ai + bi] = a[ai]; // (copy element of first array over)
            ai++;
        } else {
            pokemon[ai + bi] = b[bi]; // (copy element of second array over)
            bi++;
        }
    }
}

【问题讨论】:

  • 在这里你可以找到类似的问题link
  • 您的代码不清楚...您是要对文件或用户输入进行排序吗?您没有显示任何与任何排序方法相关的内容。我会说谷歌是你的朋友......查找递归快速排序等。
  • 很多代码与您的问题无关。请删除所有不必要的代码。
  • @JohnG 假设用户输入输入他们想要排序的口袋妖怪数量,但这并不重要。我只是不确定如何创建一个排序方法,或者只是根据它有多少个字母对一个字符串数组进行排序。解决这个问题的最佳方法是什么?我认为排序应该包括从一个位置到另一个位置的交换。这与合并排序或快速排序有关吗?
  • 我不是很确定...但是在线...sort(a, userNumber); 你永远不会超越那里。我没有看到它上面的停止条件。所以这将导致无限循环并失败。 userNumber 变量也是不必要的。

标签: java algorithm sorting recursion


【解决方案1】:

快速排序和合并排序都可以递归实现。由于这是家庭作业,我不会提供实际代码,但我会将您链接到一些您可以查看的地方:

  1. Merge Sort
  2. Quick Sort

【讨论】:

    猜你喜欢
    • 2021-06-22
    • 1970-01-01
    • 1970-01-01
    • 2018-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多