0

I want to be able to bubble sort a txt file that contains scores. I have been able to sort alphabets but not integers. I done have an idea on how to go about it. Any solution or input would be highly appreciated. Here is the bubble sort code i have already.

public static void bubbleSort(int array[]) {
        boolean swapped = true;
        while (swapped) {
            swapped = false;
            for (int i = 0; i < array.length - 1; i++) {
                if (array[i] > array[i + 1]) {
                    swapped = true;
                    int temp = array[i];
                    array[i] = array[i + 1];
                    array[i + 1] = temp;
                }
            }
            for (int i = 0; i < array.length; i++) {
                System.out.print(array[i] + "\t");
            }
            System.out.println();
        }
}
vels4j
  • 11,208
  • 5
  • 38
  • 63
Mavo Sert
  • 15
  • 8

1 Answers1

0

If there is no need for you to use Bubble Sort specifically, use the Arrays.sort() method for sorting the array.

It goes like this Arrays.sort(array);

andreih
  • 423
  • 1
  • 6
  • 19
  • This is not the answer which is expected, he wants bubble sort then you must answer the bubble sort algo – Bhavik Ambani Dec 02 '12 at 18:14
  • Specifically bubble sort. I want to read a txt file that contains integers and sort it using bubble sort. – Mavo Sert Dec 02 '12 at 19:21
  • In that case, your Bubble Sort implementation works just fine for me. Are you sure the problem is not reading the integers from the file? – andreih Dec 02 '12 at 19:29
  • 1
    [How to read integers from file](http://stackoverflow.com/questions/10752484/how-to-read-integer-values-from-text-file) – andreih Dec 02 '12 at 19:53