基礎算法系列之排序算法-2.冒泡排序

上篇文章給大家講述了二分查找算法,現在讓我們來一起學習另一個基礎算法——冒泡排序算法。它是一個排序算法,可以將一個無序的序列排成有序。它將會是我們以後常用到的算法,所以學會它,用好它對我們以後學習高級算法是很有益處的,那讓我們開始冒泡排序算法的學習吧。


冒泡排序

冒泡排序,顧名思義,就像冒泡泡一樣,不斷將小的數往上"冒"(左移),大的數不斷往下"墜落"(右移),最終得到一個一個有序的序列。它是一種簡單的排序算法。


冒泡排序的算法思想

通過兩兩比較相鄰的數,如果發現這兩個數不滿足次序要求使,則將這兩個數進行交換,從而實現"冒泡"。


冒泡排序的實現過程

從序列的最後一個數開始,不斷地將小的數往上冒,通過n-1(假設有n個數)次循壞之後,這組序列就成了一個有序的序列。


代碼實現

public static void bubbleSort (int[] a) {
        for(int i =0;i<a.length-1;i++)  //控制循壞的次數爲n-1次
            for(int j = a.length-1;j>i;j--){  //從序列的最後一個數開始,逐漸將小的數往上"冒"
                if(a[j]<a[j-1]){
                    a[j] = a[j-1]+a[j]-(a[j-1]=a[j]);   //將兩個數進行交換,也可以通過中間值temp進行交換
                }
            }
    }

運行下面代碼,讓我們來進行測試一下吧。

public static void main(String[] args) {
        int[] a = new int[]{4,3,2,1,8,6,5,10};
        bubbleSort(a);
        for(int i =0;i<a.length;i++){
            System.out.print(a[i]+"  ");
        }
        System.out.println();
    }

是不是與預期的一樣呢?!其實還有第二種實現冒泡排序的方法,就是你可以這樣想,它既然是從最後一個數開始逐漸將小的數往上"冒",那我們可不可以從第一個數開始,逐漸將大的數往下"墜落"呢?抱着這種想法讓我們來試試吧。

public static void bubbleSort(int[] a ) {
        for(int i=0;i<a.length-1;i++)  //控制循壞的次數爲n-1次
            for(int j=0;j<a.length-1-i;j++){   //從序列的第一個數開始,逐漸將大的數往下墜落
                if(a[j]>a[j+1]){
                    a[j] = a[j] + a[j+1] -(a[j+1] = a[j]);   //將兩個數進行交換,也可以通過中間值temp進行交換
                }
            }
    }

讓我們繼續用上面的測試代碼來測試看看吧。

果然,和我們預期的一樣,大家以後學習算法的時候也可以逆着想想,看看能不能達到一樣的效果,這就是所謂的逆向思維嘛,既然我們已經學了冒泡排序算法,那讓我們來做道題,試試手吧。


HDU 2689 Sort it

Problem Description

You want to processe a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. Then how many times it need. For example, 1 2 3 5 4, we only need one operation : swap 5 and 4.

Input

The input consists of a number of test cases. Each case consists of two lines: the first line contains a positive integer n (n <= 1000); the next line contains a permutation of the n integers from 1 to n.

Output

For each case, output the minimum times need to sort it in ascending order on a single line.

Sample Input

3 
1 2 3 
4 
4 3 2 1

Sample Output

0 6

問題大意:排序一個序列,並統計要進行交換的次數。

問題分析:這道題是不是讓我們想起了之前我們測試用的例子,用我們剛纔學習的冒泡排序算法,只要在兩個數交換順序的時候,添加一個計數器count,不就完成了題目的要求嘛。

代碼實現:

public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("請輸入元素個數:");
        int n = input.nextInt();
        int[] a = new int[n]; //定義數組來存儲我們輸入的值
        System.out.println("請依次輸入元素:");
        for(int i =0;i<n;i++){
            a[i] = input.nextInt();
        }
        int count=0;  //定義計數器count
        count = bubbleSort(a);
        for(int i =0;i<a.length;i++){
            System.out.print(a[i]+"  ");
        }
        System.out.println();
        System.out.println("需要交換的次數爲:"+count);
    }

    public static int bubbleSort(int[] a ) {
        int count=0; //定義計數器count
        for(int i=0;i<a.length-1;i++)  //控制循環次數爲n-1次
            for(int j=0;j<a.length-1-i;j++){ //從序列的第一個數開始,逐漸將大的數往下"墜落"
                if(a[j]>a[j+1]){
                    a[j] = a[j] + a[j+1] -(a[j+1] = a[j]); //將兩個數進行交換,也可以通過中間值temp進行交換
                    count++; //計數器自增
                }
            }
        return count;
    }

讓我們來測試一下吧。

總述

通過以上的學習,你是否學會了冒泡排序算法呢~相信以你的聰明才智肯定學會了ヾ(◍°∇°◍)ノ゙

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章