简体   繁体   English

使冒泡排序更高效

[英]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.我是编码新手,并且对如何提高效率和减少循环次数感到非常困惑。

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.简而言之,优化冒泡排序是 - 您传递n次,但在每次迭代中,您将最大(或最小)元素“冒泡”到数组末尾。 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.在这里,您可以找到不同排序算法列表,根据您的情况,这些算法可能会更有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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