本文介绍了帮助使用冒泡排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使这一要求的字符串一个人想要的数量的一个项目,然后提示按任意顺序输入字符串的人。然后,我应该按字母顺序排序他们,我不能使用的java.util的。我应该使用任何类型的排序算法,可以按字母顺序输入的字符串进行排序的。这是我的code,到目前为止,可以任何一个能帮助我把一个排序算法在我的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(你可以在这里输入的字符串:\ N);
    stringInput [I] = stdin.readLine();
    消息=信息+ stringInput [我] +,;
}
的System.out.println(你输入:\ N+消息);
}
私有静态无效冒泡(字符串[] stringInput,INT的长度){
    INT温度,计数器,指数;
    对于(计数器= 0;反<长度为1;反++){
        对于(指数= 0;指数<长度为1的柜台;指数++){
            如果(stringInput [指数]> stringInput [索引+ 1]){
                 TEMP = stringInput [指数]
                 stringInput [指数] = stringInput [索引+ 1];
                 stringInput [索引+ 1] =温度;
            }
         }
      }
   }
}
 

解决方案

为你指明方向考虑以下...

基本上它的工作方式是你有一个清单可以说在整数这种情况下

  INT unsortedNums [] =新的名单[5];

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

1,4,6,2,7

和你需要将其排序,从最低到最大。你可以这样做的方式是类似以下用伪code:

  //遍历列表
的for(int i = 0; I< unsortedNums.length;我++){
    如果(unsortedNums [1]  - ; = unsortedNums [I + 1]){//如果当前数目为<接下来的数
        //保持数那里,因为它是小于下一个数
    }其他{
        //你需要将当前数字移动到右侧,直到因为它是更大
        //比下一个数
    }
}
 

现在这不会完全排序它的第一次尝试,[在本例中,将让你的列表1,4,2,6,7],你要么必须再次调用这个函数,直到列表完全排序或者你可以看看递归,或者读了任何的各种像冒泡排序,插入排序,归并排序,依此类推。

这可能是usful提,以确保您检查您的数组界限,因为如果我+ 1大于你的清单,你会得到一个异常所以才请记住这一点。

好运与你的功课,记住,功课不只是一个东西添加到你的成绩,但功课的真正重要的是学习和实践,否则你永远不会明白和成功的任何类是。如果它是太容易了,你可能没学到什么东西。如果这是很难的,你可能会学到很多东西,即使你并不总是得到它的权利。

编码快乐。

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;
            }
         }
      }
   }
}
解决方案

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.

这篇关于帮助使用冒泡排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 16:48