java對數(shù)組進(jìn)行排序的方法
更新時間:2015年03月16日 11:06:45 作者:damaolly
這篇文章主要介紹了java對數(shù)組進(jìn)行排序的方法,涉及java數(shù)組排序的技巧,需要的朋友可以參考下
本文實例講述了java對數(shù)組進(jìn)行排序的方法。分享給大家供大家參考。具體如下:
public class Test1 {
public static void showArray(int[] array) {
for (int n = 0; n < array.length; n++) {
System.out.print(array[n]);
System.out.print(" ");
}
System.out.println();
}
public static int[] bubbleSort(int[] before) {
int t;
for (int i = 0; i < before.length; i++) {
for (int j = 0; j < before.length - i - 1; j++) {
if (before[j] > before[j + 1]) {
t = before[j];
before[j] = before[j + 1];
before[j + 1] = t;
}
}
}
return before;
}
public static void main(String[] args) {
int[] a = { 12, 24, 25, 4, 9, 68, 45, 7 };
System.out.println("排序前:");
showArray(a);
int[] b = bubbleSort(a);
System.out.println("排序后:");
showArray(b);
}
}
執(zhí)行結(jié)果:
排序前:
12 24 25 4 9 68 45 7
排序后:
4 7 9 12 24 25 45 68
希望本文所述對大家的java程序設(shè)計有所幫助。
相關(guān)文章
Spring Boot項目實戰(zhàn)之?dāng)r截器與過濾器
這篇文章主要介紹了Spring Boot項目實戰(zhàn)之?dāng)r截器與過濾器,文中給大家詳細(xì)介紹了springboot 攔截器和過濾器的基本概念,過濾器的配置,需要的朋友可以參考下2018-01-01
Apache Commons fileUpload文件上傳多個示例分享
這篇文章主要為大家分享了Apache Commons fileUpload文件上傳4個示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10
Mybatis useGeneratedKeys參數(shù)用法及問題小結(jié)
這篇文章主要介紹了Mybatis useGeneratedKeys參數(shù)用法及遇到的問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05

