Java基礎之冒泡法的實現,沒有注釋,不過應該還好理解。
啥也不多說了,直接上代碼。
- /**
- * 冒泡法排序
- */
- public class MaoPao {
-
- public static void main(String[] args) {
- maopao();
- }
-
- public static void maopao(){
- int[] x = {12,22,34,14,1,9,2};
- for(int m =0;m<x.length;m++){
- System.out.print(x[m]+", ");
- }
- System.out.println();
-
- for(int i = 0;i < x.length; i++){
- for(int j = 0;j < x.length-i-1; j++){
- if(x[j] > x[j+1]){
- int temp = x[j];
- x[j] = x[j+1];
- x[j+1] = temp;
- }
- }
- }
-
- for(int k = 0;k < x.length; k++){
- System.out.print(x[k]+", ");
- }
- }
-
- }