Java 冒泡排序實現及口訣__Java

來源:互聯網
上載者:User

視頻解析  https://edu.csdn.net/course/play/7813

在電視劇中士兵排隊時,按個子高的和個子矮的的順序進行排列,個子高的和個子矮的會進行兩兩進行比較。

冒泡排序:

其基本思想是重複的進行整個數組的排序,一次比較兩個元素(兩兩排序),如果它們順序不符合就交換,重複這樣直到數列沒有再需要交換的數為止(結束條件)。

因為它就好像氣泡一樣,輕的氣泡會往上漂浮,在不斷漂浮的過程中,發生了兩兩交換過程,所以叫冒泡排序。

/** *  * @author 張晨光 * */public class SortDemo {//公用靜態成員變數public static int[]arr={5,2,6,7,3};public static int[] sort(int[]arr){int n=arr.length;int temp;for(int i=0;i<n;i++){for(int j=0;j<n-1;j++){if(arr[j]>arr[j+1]){//交換temp=arr[j];arr[j]=arr[j+1];arr[j+1]=temp;}}}return arr;}/** * 插入排序 */public static void insertSort(){}/** * 順序輸出數組元素; */public static void printArray(int []arr){for(int num:arr){System.out.print(num+"\t");}System.out.println();}/** * main方法 */public static void main(String[] args) {SortDemo.sort(arr);SortDemo.printArray(arr);}}

應對面試排序的口訣記憶法:

N個數字來排序,

兩兩比較小靠前;

外層迴圈n-1,

內層迴圈n-1-9;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.