冒泡排序思想就是将数列的相邻两个数比较,较大的数往后保存,小的数往前。
package Sort; import java.util.Arrays; public class BubbleSort { public static int[] sort(int[] list){
int[] sortlist = list;
int temp;
for(int i = sortlist.length; i > 0; i--){
for(int j = 0; j < i-1; j++){
if(sortlist[j]>sortlist[j+1]){
temp = sortlist[j+1];
sortlist[j+1] = sortlist[j];
sortlist[j] = temp;
}
}
} return sortlist;
} public static void main(String[] args) {
int[] list = {5,4,3,2,1,10,2,3,1};
System.out.println(Arrays.toString(sort(list)));
}
}