熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

Java冒泡排序法

2013-11-23 18:48:39  來源: Java核心技術 

  public class MySort {

  public static void main(String[] args) {

  MySort sort = new MySort();
            int[] arr  = new int[]{};
            sortsort(arr);
            for(int i : arr){
                Systemoutprint(i+);
            }
        }

  public void sort(int[] targetArr){//小到大的排序

  int temp = ;
            for(int i = ;i<targetArrlength;i++){
                for(int j = i;j<targetArrlength;j++){

  if(targetArr[i]>targetArr[j]){

  /*//方法一
                        temp = targetArr[i];
                        targetArr[i] = targetArr[j];
                        targetArr[j] = temp;

  //方法二:
                        targetArr[i] = targetArr[i] + targetArr[j];
                        targetArr[j] = targetArr[i] targetArr[j];
                        targetArr[i] = targetArr[i] targetArr[j];*/

  //方法三
                        targetArr[i] = targetArr[i]^targetArr[j];
                        targetArr[j] = targetArr[i]^targetArr[j];
                        targetArr[i] = targetArr[i]^targetArr[j];

  }

  }
            }
        }

  }
    測試
    //拿位來表示吧(正規位)
    Java代碼

  int a = b = ;
              a = a + b;//
              b = a b;//
              a = a b;//=
             Systemoutprintln(a+  +b);
     //**********************************************************
             int c=;//
             int d=;//
             c=c^d;//   
             Systemoutprintln(c);
             d=c^d;//   
             c=c^d;//   

  Systemoutprintln(c+  +d);


From:http://tw.wingwit.com/Article/program/Java/hx/201311/25774.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.