Алгоритм сортировки с IOException после точного количества проходов, независимо от ввода данных - PullRequest
0 голосов
/ 03 октября 2011

Я пытаюсь использовать пузырьковую сортировку (я знаю, что она очень неэффективна) для сортировки некоторых данных, но мой код ведет себя довольно странно, после ровно 926 проходов внешнего цикла while создается IOException, это не зависит отданные введены, я проверил, и они, кажется, не связаны с объемом доступной памяти, код и исключение ниже:

public static void sort(String f1, String f2) throws FileNotFoundException, IOException {


    RandomAccessFile reader = new RandomAccessFile(f1,"rw");
    RandomAccessFile writer = new RandomAccessFile(f1,"rw");
        // start the bubble sort    
    int limit = (int) reader.length()/4;
    while (limit>1) {   
        DataOutputStream writerstream = new DataOutputStream(
                new BufferedOutputStream(new FileOutputStream(writer.getFD())));
        DataInputStream readerstream = new DataInputStream(
                new BufferedInputStream(new FileInputStream(reader.getFD())));

        // the first value, a is the first value in the file
        int a = readerstream.readInt();
        int myfilepointer = 4;
        // this flag is used to stop passing through when correct order is detected
        boolean setpass = false;
        // pass through the file

        while (myfilepointer<limit*4) {
            // the second value, b is the next value in the file
            //System.out.println(myfilepointer);

            int b = readerstream.readInt();

            myfilepointer += 4;
            // test if a and b are in the correct way around
            // if wrong way around then b is written and the next a value is the same as before
            if (a>b) { writerstream.writeInt(b);  setpass = false; }
            // if the correct way about then a is written and the next a value is the same as the previous b value
            else {  writerstream.writeInt(a);    a = b; }

        }
    // this last value is the a value of exiting the while loop
    writerstream.writeInt(a);   

    // write the new data to the file
    writerstream.flush();

    writer.seek(0);
    reader.seek(0);

    // if there was no swaps then the order is correct so exit loop
    if (setpass == true) break; 
    limit -= 1;
    }

    }

, а исключение ниже

Exception in thread "main" java.io.IOException: Read error
    at java.io.FileInputStream.readBytes(Native Method)
    at java.io.FileInputStream.read(Unknown Source)
    at java.io.BufferedInputStream.fill(Unknown Source)
    at java.io.BufferedInputStream.read(Unknown Source)
    at java.io.DataInputStream.readInt(Unknown Source)
    at uk.ac.cam.hh360.fjava.tick0.ExternalSort.sort(ExternalSort.java:48)
    at uk.ac.cam.hh360.fjava.tick0.ExternalSort.main(ExternalSort.java:119)

Ответы [ 2 ]

1 голос
/ 03 октября 2011

Одна потенциальная проблема заключается в том, что вы не закрываете потоки, открытые во внешнем цикле.

0 голосов
/ 03 октября 2011

Вы пишете в тот же файл, который читаете.Поэтому, как только вы записываете свое первое целое число в поток записи, содержимое файла заменяется тем, что вы написали.Поскольку вы используете BufferedOutputStream, на самом деле это может произойти позже, чем при первой записи.

Вы должны прочитать все целые числа в памяти, закрыть входной поток, отсортировать целые числа в памяти изатем запишите все целые числа в файл.

Я не понимаю, почему вы используете файл с произвольным доступом только для получения дескриптора файла и открытых потоков в файле.Либо вы хотите получить к нему доступ случайно, либо вы хотите получить доступ к нему с помощью потоков.Но вы не можете сделать оба.

...