开发者

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.ou开发者_如何学运维t.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().

  2. You should only call BubbleSort() once, after you've read in all the numbers from the file. Meaning, move the call outside of the while loop.

  3. You never increment the variable i so you're just overwriting myList[0] each time through your while loop.

  4. Arrays are not resizable. If you try to assign to myList[1] or myList[2] you will get an ArrayIndexOutOfBoundsException error. There are several ways to solve this--one is to change it from int[] myList = {100} to ArrayList myList = new ArrayList(). You can add numbers to it with myList.add(number) and look them up with 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. 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. 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).

In general, and especially for a beginner, it is better to use ArrayList than a regular array. 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

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. 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.


http://www.leepoint.net/notes-java/data/arrays/32arraybubblesort.html <- some bubble sort example for you ;)


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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜