java冒泡排序代碼

來源:互聯網
上載者:User

java冒泡排序代碼

冒泡排序演算法的運作如下:
  1. 比較相鄰的元素。如果第一個比第二個大,就交換他們兩個。
  2. 對每一對相鄰元素作同樣的工作,從開始第一對到結尾的最後一對。在這一點,最後的元素應該會是最大的數。
  3. 針對所有的元素重複以上的步驟,除了最後一個。
  4. 持續每次對越來越少的元素重複上面的步驟,直到沒有任何一對數字需要比較
package com.li.test;public class TestSort1 {public static void main(String[] args) {int arr[] = { 0, 9, 8, 7, 6, 5, 4, 3, 2, 1 };int temp = 0;//第一次外迴圈 結束後 最大的 跑到最後面,第二次外迴圈 結束後次大的 跑到倒數第二個,以此類推for (int i = 0; i < 10; i++) {for (int j = 0; j < 9 - i; j++) {//此處 是9-iif (arr[j] > arr[j + 1]) {temp = arr[j + 1];arr[j + 1] = arr[j];arr[j] = temp;}}}for (int i = 0; i < 10; i++) {System.out.print(arr[i]);}}}

聯繫我們

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