【问题标题】:Java Bucket Sort program for Strings用于字符串的 Java 桶排序程序
【发布时间】:2018-12-16 18:31:43
【问题描述】:

我遇到的主要问题是弄清楚如何将特定的字符串传递给存储桶类,以将每个字符串分隔到各自的存储桶中。这是一个任务,我被卡住了。我不需要解决方案,只需要一些帮助来为我指明正确的方向。

注意:我必须在bucket类中使用bucket arraylist和linkedlist

import java.util.*;
import java.io.*;

public class Bucket {
    private char minInitial;
    private char maxInitial;
    private LinkedList<String> list;

    public Bucket (char min, char max) {

        minInitial = min;
        maxInitial = max;
        list = new LinkedList<String>();

    }

    public static void main (String args[]) {

        /* There are usually 8 for the entire alphabet because letters like c and s have more words for them than others */
        ArrayList<Bucket> buckets = new ArrayList<>(8);

        buckets.add(new Bucket('a', 'b'));
        buckets.add(new Bucket('c', 'c'));
        buckets.add(new Bucket('d', 'f'));
        buckets.add(new Bucket('g', 'k'));
        buckets.add(new Bucket('l', 'o'));
        buckets.add(new Bucket('p', 'r'));
        buckets.add(new Bucket('s', 's'));
        buckets.add(new Bucket('t', 'z'));


        File inFile;

        PrintWriter outFile;
        try {

            inFile = new File("input.txt");//input.txt file has the words sepepatated by comma "cat,dog,boy,bye"

            String path = inFile.getAbsolutePath();

            //outFile = new PrintWriter("output.txt");


            System.out.println("File Name:  "+ inFile +  "File Path: " + path);

            //System.out.println("File Name: " + outFile);

            Scanner fileScan = new Scanner(inFile);

            String inputFile="";

            while (fileScan.hasNext()) {

                inputFile = fileScan.next();
                System.out.println(inputFile);
            }


            String arrayString[] = inputFile.split(",");

            Arrays.sort(arrayString); //sorts the strings alphabetically

            for (int i =0; i<arrayString.length; i++) {
                System.out.println("List elem " + i + ": " +arrayString[i]);
                //traverses every element
            }

            System.out.println(Arrays.toString(arrayString));


            List listString = Arrays.asList(arrayString);

            System.out.println(listString);


        }
        catch (FileNotFoundException e) {
            System.out.println("! ! ! File Not Found ! ! !");
            e.printStackTrace();
        }
    }
}

【问题讨论】:

  • 我从您的代码中可以理解的是,您想从文件中读取单词并将它们添加到各个存储桶的 LinkedList 列表中。对吗?
  • 您可以为每个存储桶创建一个方法,接收一个单词/字符串 iterates from char min to char max 并检查单词/字符串的第一个字母是否与当前字符匹配。
  • 是的,Punit,这就是我必须做的,但我不确定如何去做。我正在考虑一种桶的方法,它只添加从桶范围内落下的单词并将它们添加到那里

标签: java string bucket-sort


【解决方案1】:

如果您使用的是 Java 8,您可以执行以下操作来将单词添加到相应的存储桶中:

Arrays.stream(arrayString)
.forEach(word -> buckets.stream()
    .filter(bucket -> bucket.minInitial <= word.charAt(0) && word.charAt(0) <= bucket.maxInitial)
    .findFirst().ifPresent(bucket -> bucket.list.add(word)));

【讨论】:

    【解决方案2】:

    我不建议在桶中混合字母。 你不应该像以前那样做任何排序:

    Arrays.sort(arrayString); //按字母顺序对字符串进行排序

    简单算法:
    1. 求最长字符串的长度 (O(n))
    2.从上面的长度开始往回走,在每次迭代中将Strings放入桶中(就像对数字进行桶排序)(O(Length * n))

    我没有在这里放任何代码,因为这是一个作业

    【讨论】:

    • OP 的帖子中有一条注释消息:“注意:我必须在存储桶类中使用存储桶数组列表和链接列表”。可能该课程是关于理解桶列表机制(例如,使用哈希图很有用)
    猜你喜欢
    • 1970-01-01
    • 2011-07-22
    • 2014-10-01
    • 2021-03-21
    • 2014-12-06
    • 1970-01-01
    • 1970-01-01
    • 2012-01-17
    • 1970-01-01
    相关资源
    最近更新 更多