有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使气泡排序更有效

我有下面的气泡排序代码。我想知道我怎样才能运行得更高效,循环次数更少

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

任何帮助都将不胜感激。我对编码还不熟悉,对于如何提高效率和减少循环次数一直很困惑


共 (1) 个答案

  1. # 1 楼答案

    气泡排序是一种效率很低的排序算法,有很多更好的排序算法

    你可以使冒泡排序更有效,它被称为优化冒泡排序(它仍然非常低效)

    优化的冒泡排序简言之是,-传递n次,但在每次迭代中,将最大(或最小)的元素“冒泡”到数组的末尾。现在,最后一项已排序,因此您无需再次进行比较

    我去年写了这段代码,不确定它是否仍然有效:

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

    你可以在这里阅读optimized bubble sort

    在这里,你可以找到一个list of different sorting algorithms,根据你的场景,它可能更有效