冒泡排序(Java版)

來源:互聯網
上載者:User

標籤:排序演算法

冒泡排序(Bubble Sort)是一種簡單的排序演算法。它重複地走訪過要排序的數列,一次比較兩個元素,如果他們的順序錯誤就把他們交換過來。走訪數列的工作是重複地進行直到沒有再需要交換,也就是說該數列已經排序完成。這個演算法的名字由來是因為越小的元素會經由交換慢慢“浮”到數列的頂端。

冒泡排序演算法的運作如下:
比較相鄰的元素。如果第一個比第二個大,就交換他們兩個。 對每一對相鄰元素作同樣的工作,從開始第一對到結尾的最後一對。在這一點,最後的元素應該會是最大的數。
針對所有的元素重複以上的步驟,除了最後一個。持續每次對越來越少的元素重複上面的步驟,直到沒有任何一對數字需要比較。

package BubbleSort;import java.util.Arrays;public class Demo01 {    //第一版本     static void BubbleSort(int[] arr){        int len = arr.length;        for(int j=0;j<len-1;j++){            for(int i=0;i<len-1;i++)            {                if(arr[i]>arr[i+1]){                    int temp = arr[i];                    arr[i] = arr[i+1];                    arr[i+1] = temp;                }            }            System.out.println(Arrays.toString(arr));        }    }    //第二版本  每一趟減少每一趟的次數     static void BubbleSortSecond(int[] arr){        int len = arr.length;        for(int j=0;j<len-1;j++)        {            for(int i=0;i<len-1-j;i++)            {                if(arr[i]>arr[i+1])                {                    int temp = arr[i];                    arr[i] = arr[i+1];                    arr[i+1] = temp;                }            }            System.out.println(Arrays.toString(arr));        }           }    //第三版本    /**     * 最終版:假設資料已經有序,後者經過某趟後有序,減少趟數     * 如 81234     * 第一趟  12348     * 第二趟  12348     * @param args     */    static void BubbleSortThird(int[] arr){             boolean sorted = true;        int len = arr.length;        for(int j=0;j<len-1;j++)        {            sorted = true;//假定有序            for(int i=0;i<len-1-j;i++)            {                               if(arr[i]>arr[i+1])                {                    int temp = arr[i];                    arr[i] = arr[i+1];                    arr[i+1] = temp;                    sorted = false;//假定失敗                }            }            System.out.println(Arrays.toString(arr));            if(sorted){                break;            }        }           }    public static void main(String[] args) {            System.out.println("===========1============");        int[] arr = {9,2,7,3,4};        BubbleSort(arr);        System.out.println("===========2============");        arr = new int[] {9,1,7,3,4};        BubbleSortSecond(arr);        System.out.println("===========3============");        arr = new int[] {9,1,7,3,4};        BubbleSortThird(arr);    }}

運行結果:

===========1============[2, 7, 3, 4, 9][2, 3, 4, 7, 9][2, 3, 4, 7, 9][2, 3, 4, 7, 9]===========2============[1, 7, 3, 4, 9][1, 3, 4, 7, 9][1, 3, 4, 7, 9][1, 3, 4, 7, 9]===========3============[1, 7, 3, 4, 9][1, 3, 4, 7, 9][1, 3, 4, 7, 9]

冒泡排序(Java版)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.