简体   繁体   English

如何修复这个冒泡排序程序?

[英]How to fix this bubble sort program?

package arraySort;

import java.io.IOException;
import java.io.File;
import java.util.*;

public class openFile {
    int x;
    static int i;
    static int[] myList = {100};

    public static void main(String[] args){
        try{
            File myFile = new File("arraySort.txt");
            Scanner scan = new Scanner(myFile);
            while(scan.hasNext()){                
                myList[i] = scan.nextInt();
                BubbleSort(myList);
                System.out.println(myList[i]);                                
         } 
         catch(IOException e){
             System.out.println("File not found!");
         }
    }
    public static void BubbleSort(int[] x){
        if (x[i] > x[i + 1]){
            int temp;
            temp = x[i];
            x[i] = x[i+1];
            x[i+1] = temp;
        }
    }
}

Rather than give you the answer outright, here are a couple of hints:这里没有直接给你答案,这里有一些提示:

  1. You don't have any loops in BubbleSort() .您在BubbleSort()没有任何循环。

  2. You should only call BubbleSort() once, after you've read in all the numbers from the file.在读取文件中的所有数字,您应该只调用一次BubbleSort() Meaning, move the call outside of the while loop.意思是,将调用移到while循环之外。

  3. You never increment the variable i so you're just overwriting myList[0] each time through your while loop.你永远不会增加变量i所以你每次通过你的while循环你只是覆盖myList[0]

  4. Arrays are not resizable.数组不可调整大小。 If you try to assign to myList[1] or myList[2] you will get an ArrayIndexOutOfBoundsException error.如果您尝试分配给myList[1]myList[2]您将收到ArrayIndexOutOfBoundsException错误。 There are several ways to solve this--one is to change it from int[] myList = {100} to ArrayList myList = new ArrayList() .有几种方法可以解决这个问题——一种是将它从int[] myList = {100}更改为ArrayList myList = new ArrayList() You can add numbers to it with myList.add(number) and look them up with myList.get(i) .您可以使用myList.add(number)添加数字,并使用myList.get(i)查找它们。

Your program has several problems, not just related to the sorting part.你的程序有几个问题,不仅仅与排序部分有关。

static int[] myList = {100};

This line defines myList as an array with size 1, containing the single element 100 .这一行将myList定义为一个大小为 1 的数组,包含单个元素100 Then, your main loop is然后,你的主循环是

while(scan.hasNext()) {
    myList[i] = scan.nextInt();
    BubbleSort(myList);
    System.out.println(myList[i]);
}

You do not increase i in this loop so you are just overwriting the single value in myList with whatever value you read from the file.你不会在这个循环中增加i ,所以你只是用你从文件中读取的任何值覆盖myList的单个值。 And when your Bubblesort function tries to access myList[i+1] , it throws an ArrayIndexOutOfBoundsException because there is no element at index i+1 (which equals 1 , since you don't increase i ).当您的Bubblesort函数尝试访问myList[i+1] ,它会抛出一个ArrayIndexOutOfBoundsException因为索引i+1处没有元素(等于1 ,因为您没有增加i )。

In general, and especially for a beginner, it is better to use ArrayList than a regular array.一般来说,尤其是对于初学者来说,使用ArrayList比使用常规数组更好。 Also, you should fill in the array first and only after it has all the elements should you attempt to sort it.此外,您应该首先填充数组,并且只有在它拥有所有元素之后才尝试对其进行排序。 Finally, it is a better idea to make the variables local instead of class members.最后,将变量设置为局部变量而不是类成员是一个更好的主意。 So that would make your main function something like所以这会让你的main功能类似于

ArrayList myList = new ArrayList();
while(scan.hasNext()) {
    myList.append(scan.nextInt());
}
Bubblesort(myList);

And then change Bubblesort to take an ArrayList , and then you can also make the loop index i local to the Bubblesort method.然后将Bubblesort更改为采用ArrayList ,然后您还可以使循环索引i位于Bubblesort方法的本地。 After that is done, you can work on getting the bubble sort algorithm working.完成后,您可以开始使用冒泡排序算法。 Remember to be careful with your array indices there so that you never access outside the bounds of the array.记住要小心你的数组索引,这样你就永远不会访问数组的边界之外。

Change this:改变这个:

 try{
    File myFile = new File("arraySort.txt");
    Scanner scan = new Scanner(myFile);
    while(scan.hasNext()){                
        myList[i] = scan.nextInt();
        BubbleSort(myList);
        System.out.println(myList[i]);
 } 
 catch(IOException e){
     System.out.println("File not found!");
 }

to:到:

try{
    File myFile = new File("arraySort.txt");
    Scanner scan = new Scanner(myFile);
    while(scan.hasNext()){                
        myList[i] = scan.nextInt();
 } 
 catch(IOException e){
     System.out.println("File not found!");
 }

 BubbleSort(myList);
 System.out.println(myList[i]);

} }

Change sort method according to answer by @Federico根据@Federico 的回答更改排序方法

When you have finished this assignment you might appreciate Collections.sort() :) 完成分配后,您可能会喜欢Collections.sort():)

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Collections.html#sort%28java.util.List%29 http://java.sun.com/j2se/1.4.2/docs/api/java/util/Collections.html#sort%28java.util.List%29

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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