java學習-氣泡排序
阿新 • • 發佈:2018-12-29
思想很簡單,實現也很簡單,小的放前面,大的放後面,兩兩比較即可,下面是程式碼
package maopao;
import java.util.Arrays;
public class maopao{
int[] numbers= new int[] {1,2,5,4,2,5,7,3,5};
public static void main (String[] args) {
maopao mp = new maopao();
mp.maopaopaixu();
System.out.println(Arrays.toString(mp.numbers));
}
public void maopaopaixu() {int temp;
int len = numbers.length;
System.out.println(len);
System.out.println(Arrays.toString(numbers));
for(int i=0;i<len-1;i++) {
for (int j=i+1;j<len;j++) {
if(numbers[i]>=numbers[j]) {
temp=numbers[i];
numbers[i]=numbers[j];
numbers[j]=temp;
}
}
}
}
}