-3

I'm trying to make a project that asks for the number of strings a person wants, then prompts the person to enter the strings in any order. Then I am supposed to order them alphabetically and I CAN NOT use Java.util at all. I am supposed to use any type of sorting algorithm that can sort the inputted strings in alphabetical order. This is my code so far, could any one help me put a sorting algorithm in my code?

package sorting;
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];
String message = "";
for(int i = 0; i < stringCount; i++)
{
System.out.print("Could you enter the strings here: \n");
    stringInput[i] = stdin.readLine();
    message = message + stringInput[i] + ", ";
}
System.out.println("So you entered:\n" + message);    
}
private static void bubbleSort(String[] stringInput, int length) {
    int temp, counter, index;
    for(counter=0; counter<length-1; counter++) {
        for(index=0; index<length-1-counter; index++) {
            if(stringInput[index] > stringInput[index+1]) {
                 temp = stringInput[index];
                 stringInput[index] = stringInput[index+1];
                 stringInput[index+1] = temp;
            }
         }
      }
   }
}
5
  • 3
    No. Your homework is your own endeavor. If you don't want to learn any sorting, then just google and copy-paste. Commented Aug 2, 2011 at 19:59
  • 1
    take a few stabs at it yourself and come back and show us where you have failed ... Commented Aug 2, 2011 at 20:01
  • See also stackoverflow.com/questions/6905947/… (but this was without the restriction "not use java.util", thus it is not an exact duplicate). Commented Aug 2, 2011 at 20:01
  • You can find lots of information about sorting algorithms on Wikipedia Commented Aug 2, 2011 at 20:01
  • If you post an almost-complete solution, with a bug that you can't find, I expect that you will find others willing to help you find it. But you have shown no effort to begin sorting anything, so no one is helped if we do it for you. Commented Aug 2, 2011 at 20:04

3 Answers 3

3
  1. Look up what a sorting algorithm does, and select one from the list.

  2. Then implement the algorithm (preferably in a separate method).

  3. Then call this method from your main method after the input loop.

If you have a problem with 2. or 3., post (by editing the question) what you tried, and what your problem is (e.g. any error messages or wrong versus expected output), and we can help you.

Sign up to request clarification or add additional context in comments.

Comments

3

Try using bubble sort, insertion sort or merge sort, you can Google them to learn more about them.

Also, Scanner is so much nicer than BufferReader sometimes. but since it is in util i guess you can't use it.

Comments

2

To point you in a direction think about the following...

Basically the way it works is you have a list lets say in this case of ints

int unsortedNums[] = new list[5];

unsortedNums[0] = 1;
unsortedNums[1] = 4; 
unsortedNums[2] = 6; 
unsortedNums[3] = 2; 
unsortedNums[4] = 7; 

1, 4, 6, 2, 7

and you need to sort it from least to greatest. The way you would do this is like the following using pseudo code:

//loop through the list
for(int i = 0; i < unsortedNums.length; i++){
    if(unsortedNums[i] <= unsortedNums[i+1]){ //if current number is < next number
        //keep the number there because it is less than the next number
    }else{
        //you need to shift the current number to the right until because it is bigger
        //than the next number
    }
}

now this won't completely sort it on the first try, [in this example that would make your list 1, 4, 2, 6, 7] you would either have to call this function again until the list is fully sorted, or you could look into recursion, or read up about any of the sorts like bubble sort, insertion sort, merge sort, and so on.

It might be usful to mention to make sure you check your Array Bounds because if i+1 is bigger than your list you will get an exception so just keep this in mind.

Good Luck with your Homework and remember, homework isn't just another thing to add to your grade, but the real importance of homework is to learn and practice or else you will never understand and succeed in whatever class it is. If it is too easy, you probably didn't learn anything. If it is hard, you probably will learn a lot even if you don't always get it right.

Happy Coding.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.