国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java排序算法之哪一種好

2019-11-06 09:13:19
字體:
來源:轉載
供稿:網友

歡迎討論 回復 哪個算法+原因 即可

package Sort;public class Sort { /** * 算法A */ public static void sortA() { int a[] = { 10, 2, 3, 6, 5, 4, 9, 7, 1, 8 }; for (int i = 0; i <=a.length - 1; i++) { for (int j = i + 1; j<=a.length - 1; j++) { if (a[j] <= a[i]) { int c = a[i]; a[i] = a[j]; a[j] = c; } } } for (int i = 0; i <= a.length - 1; i++) { System.out.PRintln(a[i]); } } /** * 算法B */ public static void sortB() { int a[] = { 10, 2, 3, 6, 5, 4, 9, 7, 1, 8 }; for (int i = 0; i <=a.length - 1; i++) { for (int j = 0; j< a.length - 1 - i; j++) { if (a[j] >a[j+1]) { int c = a[j]; a[j] = a[j+1]; a[j+1] = c; } } } for (int i = 0; i <= a.length - 1; i++) { System.out.println(a[i]); } } /** * 算法C */ public static void sortC(){ int a[] = { 10, 2, 3, 6, 5, 4, 9, 7, 1, 8 }; int length = a.length; while(true) { boolean isEnd = true; for(int i = 0; i < length - 1 ; i++) { // 標識是否全部交換完畢 int before = a[i]; int behind = a[i + 1]; if(behind < before) { // 如果后面數的值<前面數的值,則交換 a[i] = behind; a[i + 1] = before; isEnd = false; // 沒有交換完畢 continue; } else if (i == length - 1) { // 如果最后一個人元素的場合下 還沒有發生可以交換的事件 isEnd = true; } } if(isEnd) {break;} } for(int n : a) { System.out.print(n + ", "); } } public static void main(String[] args) { Sort.sortA(); Sort.sortB(); Sort.sortC(); } }
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 吉木萨尔县| 高州市| 毕节市| 清流县| 古交市| 探索| 兴安县| 团风县| 儋州市| 扬州市| 上虞市| 策勒县| 南溪县| 景德镇市| 襄汾县| 华容县| 龙江县| 英山县| 武宁县| 新营市| 大姚县| 开阳县| 醴陵市| 西乌珠穆沁旗| 武隆县| 马尔康县| 蒙山县| 灌南县| 房产| 绥滨县| 平泉县| 柳河县| 兴安县| 江源县| 枞阳县| 静安区| 工布江达县| 平顺县| 三原县| 赣州市| 黄平县|