简体   繁体   中英

How to use a sorting algorithm in Java?

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

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.

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

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

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);
    }
}

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.

  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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM