Java 實現的 DSA - 氣泡排序



概述

氣泡排序是一種簡單的排序演算法。該排序演算法是基於比較的演算法,其中比較每一對相鄰元素,並且如果它們不按順序,則交換元素。該演算法不適用於大型資料集,因為其平均時間複雜度和最壞時間複雜度為 O(n2),其中 n 為項數。

虛擬碼

procedure bubbleSort( A : array of items )
   for i = 1 to length(A) - 1 inclusive do:
      swapped = false
      for j = 1 to length(A) - 1 inclusive do:
         /* compare the adjacent elements */   
         if A[i-1] > A[i] then
            /* swap them */
            swap( A[i-1], A[i] )		 
            swapped = true
         end if
      end for
      /*if no number was swapped that means 
      array is sorted now, break the loop.*/
      if(!swapped) then
         break	 
   end for
end procedure

程式碼示例

package com.tutorialspoint.simplesort;

import java.util.Arrays;

public class BubbleSortDemo {
    
   public static void main(String[] args){
      int[] sourceArray = {4,6,3,2,1,9,7};
      System.out.println("Input Array: " 
      + Arrays.toString(sourceArray));
      printline(50);
      System.out.println("Output Array: " 
      + Arrays.toString(bubbleSort(sourceArray)));
      printline(50);
   }    

   public static void printline(int count){
      for(int i=0;i <count-1;i++){
         System.out.print("=");
      }
      System.out.println("=");
   }

   public static int[] bubbleSort(int[] intArray){

      int temp;
      boolean swapped = false;       
      // loop through all numbers 
      for(int i=0; i < intArray.length-1; i++){ 
         swapped = false;
         // loop through numbers falling ahead 
         for(int j=1; j < intArray.length-i; j++){
            System.out.println("     Items compared: [ " 
            + intArray[j-1] + ", " + intArray[j] +" ]" ); 
            // check if next number is lesser than current no
            //   swap the numbers. 
            //  (Bubble up the highest number) 
            if(intArray[j-1] > intArray[j]){
               temp=intArray[j-1];
               intArray[j-1] = intArray[j];
               intArray[j] = temp;
               swapped = true;
            }           
         }
         // if no number was swapped that means 
         //   array is sorted now, break the loop. 
         if(!swapped){
            break;
         }
         System.out.println("Iteration "+(i+1) +"#: " 
                            + Arrays.toString(intArray));
         }            
      return intArray;
   }    
}

如果我們編譯並執行上述程式碼,它將產生以下結果:

Input Array: [4, 6, 3, 2, 1, 9, 7]
==================================================
     Items compared: [ 4, 6 ]
     Items compared: [ 6, 3 ]
     Items compared: [ 6, 2 ]
     Items compared: [ 6, 1 ]
     Items compared: [ 6, 9 ]
     Items compared: [ 9, 7 ]
Iteration 1#: [4, 3, 2, 1, 6, 7, 9]
     Items compared: [ 4, 3 ]
     Items compared: [ 4, 2 ]
     Items compared: [ 4, 1 ]
     Items compared: [ 4, 6 ]
     Items compared: [ 6, 7 ]
Iteration 2#: [3, 2, 1, 4, 6, 7, 9]
     Items compared: [ 3, 2 ]
     Items compared: [ 3, 1 ]
     Items compared: [ 3, 4 ]
     Items compared: [ 4, 6 ]
Iteration 3#: [2, 1, 3, 4, 6, 7, 9]
     Items compared: [ 2, 1 ]
     Items compared: [ 2, 3 ]
     Items compared: [ 3, 4 ]
Iteration 4#: [1, 2, 3, 4, 6, 7, 9]
     Items compared: [ 1, 2 ]
     Items compared: [ 2, 3 ]
Output Array: [1, 2, 3, 4, 6, 7, 9]
==================================================
廣告