HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavaMinor

Bubble sort class in Java

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
sortbubbleclassjava

Problem

I have written this class which sorts the array using bubble sort. Please let me know how can I improve it. (I don't like recurrent code in if/else statement)

public class BubbleSort {

public static void numbers(int[] array){
    numbers(array, ' array[j + 1]){
                    int tmp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = tmp;
                }
            }
        }
    }
}

public static void letters(char[] array){
    letters(array, ' array[j + 1]){
                    char tmp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = tmp;
                }
            }
        }
    }
}   
}

Solution

Bubble Sort has an inefficient worst case. In general, you could speed things up by switching from this \$O(n^2)\$ sort to one of the \$O(n \log n)\$ sorts, e.g. Quicksort. Simplest would be to just use Arrays.sort but perhaps this is a programming exercise (tagging as reinventing-the-wheel would let reviewers know that).

for(int i = 0; i < array.length - 1; i++){
            for(int j = 0; j < array.length - i - 1; j++){

                if(array[j] < array[j + 1]){
                    int tmp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = tmp;
                }
            }
        }


The one thing that Bubble Sort can do well is handle sorted input efficiently in \$O(n)\$ time. But you don't do that.

for (int i = 0; i < array.length - 1; i++) {
            boolean bubbled = false;
            for (int j = 0; j < array.length - i - 1; j++) {
                if (array[j] < array[j + 1]){
                    int tmp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = tmp;
                    bubbled = true;
                }
            }

            if (!bubbled) {
                return;
            }
        }


Now it will return after a single pass if the input is sorted. If the input is almost sorted, it may return on the second or third pass.

You can save a calculation an outer loop iteration if you're really trying to optimize.

for (int i = array.length - 1; i > 0; i--) {
            boolean bubbled = false;
            for (int j = 0; j < i; j++) {
                if (array[j] < array[j + 1]){
                    int tmp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = tmp;
                    bubbled = true;
                }
            }

            if (!bubbled) {
                return;
            }
        }


Now you don't have to calculate array.length - 1 - i on every outer iteration.

Note that a naive compiler might actually calculate that on each inner iteration in the original code.

Code Snippets

for(int i = 0; i < array.length - 1; i++){
            for(int j = 0; j < array.length - i - 1; j++){

                if(array[j] < array[j + 1]){
                    int tmp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = tmp;
                }
            }
        }
for (int i = 0; i < array.length - 1; i++) {
            boolean bubbled = false;
            for (int j = 0; j < array.length - i - 1; j++) {
                if (array[j] < array[j + 1]){
                    int tmp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = tmp;
                    bubbled = true;
                }
            }

            if (!bubbled) {
                return;
            }
        }
for (int i = array.length - 1; i > 0; i--) {
            boolean bubbled = false;
            for (int j = 0; j < i; j++) {
                if (array[j] < array[j + 1]){
                    int tmp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = tmp;
                    bubbled = true;
                }
            }

            if (!bubbled) {
                return;
            }
        }

Context

StackExchange Code Review Q#131345, answer score: 2

Revisions (0)

No revisions yet.