網站首頁 健康小知識 母嬰教育 起名 運動知識 職場理財 情感生活 綠色生活 遊戲數碼 美容 特色美食 愛好

java編程:用冒泡排序實現升序排列和降序排列

欄目: 學習交流 / 發佈於: / 人氣:3.28W

12,45,9,67,455,用冒泡排序實現升序排列

操作方法

(01)package huo;public class Test05 {public static void main(String[] args) ;for (int i = 0; i < th - 1; i++) {for (int j = 0; j < th - 1 - i; j++) {if (arr[j] > arr[j + 1]) {int temp;temp = arr[j];arr[j] = arr[j + 1];arr[j + 1] = temp;}}}tln("排序後:");for (int i = 0; i < th; i++)t(arr[i] + "t");}}

java編程:用冒泡排序實現升序排列和降序排列

特別提示

若要實現降序排列,只需將arr[j] &gt; arr[j + 1]中的&quot;&gt;&quot; 改為&quot;