如何在Java中使用排序算法?

发布于 2024-11-27 12:54:12 字数 728 浏览 2 评论 0 原文

我的任务是询问用户他想要输入的字符串数量。然后我会提示他纠正琴弦。然后我应该按字母顺序打印刺痛。我完成了大部分作业,只需要一个排序算法(例如冒泡排序)来完成它。这是我的代码。

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?
  }
}

My assignment is to ask a user for the number of strings he would like to input. Then i will prompt him to right the strings. Then i am supposed to print the stings in alphabetical order. I got most of the assignment done, just need a sorting algorithm such as Bubble sort to finish it. this is my code.

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

允世 2024-12-04 12:54:12

使用 Rhino 作为 Javascript 解析器,这样您就可以包含 jQuery 到您的项目中。然后排序就变得微不足道了,因为您只需将 String 加载到 中并运行此 漂亮的插件就可以了。

^ DO NOT DO THIS. (well if you do, post the source and tell us what grade you got on the assignment ;)

实在不行,自己写个冒泡排序就可以了。没那么长。您可能在课堂上学习了伪代码。如果您需要其他参考,请查看相关的维基百科文章。如果您对算法有什么特别不明白的地方,请发布具体问题,我们将为您提供帮助。除此之外,到目前为止你看起来走在正确的轨道上:)

Use Rhino as a Javascript parser so you can include jQuery into your project. Then sorting becomes trivial as you can just load the Strings into a <table> and run this nifty plugin on it.

^ DO NOT DO THIS. (well if you do, post the source and tell us what grade you got on the assignment ;)

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 :)

香橙ぽ 2024-12-04 12:54:12

如果这不是练习,您只需使用 Arrays.sort(stringInput) 即可

If this wasn't an exercise you'd just use Arrays.sort(stringInput)

染年凉城似染瑾 2024-12-04 12:54:12
import java.io.*;
import java.util.*;
public class sorting
{
    public static Scanner input = new Scanner(System.in);
    public static void main(String[] args) throws IOException
    {
         System.out.print("How many strings would you like to enter? ");
         String[] stringInput = new String[Integer.parseInt(input.nextLine())];
         for(int i = 0; i < stringInput.length; i++)
         {
             System.out.print("Could you enter the strings here: ");
             stringInput[i] = input.nextLine();
         }  
         Arrays.sort(stringInput);
         for(String s : stringInput) System.out.println(s);
    }
}
import java.io.*;
import java.util.*;
public class sorting
{
    public static Scanner input = new Scanner(System.in);
    public static void main(String[] args) throws IOException
    {
         System.out.print("How many strings would you like to enter? ");
         String[] stringInput = new String[Integer.parseInt(input.nextLine())];
         for(int i = 0; i < stringInput.length; i++)
         {
             System.out.print("Could you enter the strings here: ");
             stringInput[i] = input.nextLine();
         }  
         Arrays.sort(stringInput);
         for(String s : stringInput) System.out.println(s);
    }
}
苄①跕圉湢 2024-12-04 12:54:12

在我看来,您并没有被要求对字符串数组进行排序,而是按字母顺序打印字符串,这可能会更丑陋但更简单。

用户键入所有输入字符串后,您可以选择迭代数组 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.

紫罗兰の梦幻 2024-12-04 12:54:12
  1. 只需使用 Set str = TreeSet();
  2. 然后循环遍历字符串 str.add("z");
  3. 当您迭代 str 变量时,它会自动排序。
  1. Just use the Set str = TreeSet();
  2. then loop through the string str.add("z");
  3. when you iterate the str variable it is automatically sorted.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文