Joey Rudd Student
Joey Rudd Student

Reputation: 1

Make Bubble Sorting more Efficient

I have the code for bubble sorting down below. I was wondering how I would be able to run more efficient and loop less amount of times

package bubbleSort;

public class BubbleSort {

    public static void main(String[] args) {
        
        // initialize array to sort
        int size = 10;
        int[] numbers = new int[size];
 
        // fill array with random numbers
        randomArray(numbers);
 
        // output original array
        System.out.println("The unsorted array: ");
        printArray(numbers);
        
        // call the bubble sort method
        bubbleSort(numbers);
        
        // output sorted array
        System.out.println("The sorted array: ");
        printArray(numbers);


    }
    
     public static void bubbleSort(int tempArray[]) {
            
            //bubble sort code goes here (you code it) 
            
            // loop to control number of passes
            for (int pass = 1; pass < tempArray.length; pass++) {
                System.out.println(pass);
                // loop to control number of comparisions for length of array - 1
                for (int element = 0; element < tempArray.length - 1; element++) {
                    
                    // compare side-by-side elements and swap tehm if
                    // first element is greater than second elemetn swap them
                    if (tempArray [element] > tempArray [element + 1]) {
                        swap (tempArray, element, element + 1);
                        
                    }
                }
            }
        }
     
       public static void swap(int[] tempArray2,int first, int second) {
            
           //swap code goes here (you code it) 
           
           int hold; // temporary holding area for swap
           
           hold = tempArray2 [first];
           tempArray2 [first] = tempArray2 [second];
           tempArray2 [second] = hold;
           
        }

       public static void randomArray(int tempArray[]) {
           
            int count = tempArray.length;
     
            for (int i = 0; i < count; i++) {
                tempArray[i] = (int) (Math.random() * 100) + 1;
            }
        }

       public static void printArray(int tempArray[]) {
           
            int count = tempArray.length;
     
            for (int i = 0; i < count; i++) {
                System.out.print(tempArray[i] + " ");
            }
            System.out.println("");
        }
}

Any help would be greatly appreciated. I am new to coding and have been very stumped on how to improve efficiency and have it loop less times.

Upvotes: 0

Views: 3184

Answers (1)

Karl H
Karl H

Reputation: 130

Bubble sort is an inefficiency sorting algorithm and there are much better sorting algorithm.

You can make a bubble sort more efficient, its called Optimized Bubble Sort (It is still quite inefficient)

Optimized bubble sort in short is, - you pass n times , but on the every iteration you 'bubble' the biggest (or smallest) element to the end of the array. Now that last item is sorted, so you don't have to compare it again.

I wrote this code last year, not to sure if it still works:

 public static void bubbleSort_withFlag(Integer[] intArr) {
        int lastComparison = intArr.length - 1;

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

            }
            if (isSorted) return;
            lastComparison = currentSwap;
        }
    } 

Here you can read on optimized bubble sort

Here you can find a list of different sorting algorithms that could be more efficient depending on your scenario.

Upvotes: 3

Related Questions