如何使用Java中的排序算法?如何使用、算法、Java

2023-09-11 06:46:25 作者:杀死你的温柔

我的任务是向用户字符串的他想输入数字。然后,我会提示他右边的字符串。那么我应该打印蜇按字母顺序排列。我得到了大部分的任务完成了,只需要一个排序算法,如冒泡排序来完成它。这是我的code。

 进口java.io. *;
公共类排序
{


私有静态的BufferedReader标准输入=新的BufferedReader(新的InputStreamReader(System.in));

  公共静态无效的主要(字符串[]参数)抛出IOException异常
  {
     的System.out.println(有多少串你想进入?);
     INT stringCount =的Integer.parseInt(stdin.readLine());
     的String [] stringInput =新的String [stringCount]
     的for(int i = 0; I< stringCount;我++)
     {
         System.out.print(你能在此输入字符串:);
         stringInput [I] = stdin.readLine();
     }
     //现在我如何使用排序算法?
  }
}
 

解决方案

Arrays.sort()

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?
  }
}
Java常用的八种排序算法与代码实现

解决方案

Arrays.sort().