如何在Java中使用排序算法?
我的任务是询问用户他想要输入的字符串数量。然后我会提示他纠正琴弦。然后我应该按字母顺序打印刺痛。我完成了大部分作业,只需要一个排序算法(例如冒泡排序)来完成它。这是我的代码。
import java.io.*;
public class sorting
{
private static BufferedReader stdin=new BufferedReader(new InputStreamReader( System.in));
public static void main(String[] arguments) throws IOException
{
System.out.println("How many strings would you like to enter?");
int stringCount = Integer.parseInt(stdin.readLine());
String[] stringInput = new String[stringCount];
for(int i = 0; i < stringCount; i++)
{
System.out.print("Could you enter the strings here: ");
stringInput[i] = stdin.readLine();
}
//Now how do i use a sorting algorithm?
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Arrays.sort()
。Arrays.sort()
.使用 Rhino 作为 Javascript 解析器,这样您就可以包含 jQuery 到您的项目中。然后排序就变得微不足道了,因为您只需将
String
加载到中并运行此 漂亮的插件就可以了。
实在不行,自己写个冒泡排序就可以了。没那么长。您可能在课堂上学习了伪代码。如果您需要其他参考,请查看相关的维基百科文章。如果您对算法有什么特别不明白的地方,请发布具体问题,我们将为您提供帮助。除此之外,到目前为止你看起来走在正确的轨道上:)
Use Rhino as a Javascript parser so you can include jQuery into your project. Then sorting becomes trivial as you can just load the
String
s into a<table>
and run this nifty plugin on it.No really, just write bubble sort by yourself. It's not that long. You probably learned the pseudocode in class. If you need an additional reference, take a look at the Wikipedia article on it. If there's something in particular that you don't understand about the algorithm, post a specific question and we'll help you out. Other than that, you look like you're on the right track so far :)
如果这不是练习,您只需使用 Arrays.sort(stringInput) 即可
If this wasn't an exercise you'd just use
Arrays.sort(stringInput)
在我看来,您并没有被要求对字符串数组进行排序,而是按字母顺序打印字符串,这可能会更丑陋但更简单。
用户键入所有输入字符串后,您可以选择迭代数组
stringCount
次,在每次迭代中查找值最低的字符串;打印它;然后清除它,以便您在下一次迭代中不会看到它。但是如果你真的被要求对数组应用冒泡排序(或任何类型的排序),那么,这就是一个完整的“另一个问题”,即:如何做我为字符串数组编写了冒泡排序。这对任何人来说都是一个棘手的问题,因为数组中的字符串具有不同的长度:它们不能只是盲目地交换,而是必须写入某个临时数组中,该数组以某种方式知道如何适应它们的不同长度。
编辑:哦,等一下:也许Java知道所有关于可变长度字符串以及如何处理它们的信息。如果是这样,我就把一切收回来。
It seems to me that you are not being asked to sort the string array, but to print the strings in alphabetical order, which is likely to be a whole lot uglier but a whole lot simpler.
Once the user has typed all of the input strings, you might choose to iterate over the array
stringCount
times finding, in each iteration, the lowest-valued string; printing it; and then clearing it so that you won't see it in the next iteration.BUT if you really are being asked to apply a bubble sort (or any kind of sort) on the array, well, that's a whole 'nother question, namely: how do I write a bubble sort for an array of strings. That's a tricky problem for anyone because the strings in the array are of differing lengths: they cannot just be swapped blindly but have to be written into some temp array somewhere that somehow knows how to accomodate their various lengths.
EDIT: Oh, wait a sec: maybe Java knows all about variable-length strings and how to handle them. If so, I take it all back.