简体   繁体   中英

Bubble Sort .txt file containing integers in java

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();
        }
}

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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