开发者

Help using Bubble Sort [closed]

开发者 https://www.devze.com 2023-03-24 12:59 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

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开发者_StackOverflow社区.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;
            }
         }
      }
   }
}


  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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消