`
guoyaxu
  • 浏览: 94312 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类

java 冒泡算法

阅读更多
java 冒泡算法
引自:damies
链接:http://damies.iteye.com/blog/51775

java 代码
 
  1. package ce;  
  2.   
  3. public class BubbleSort {  
  4.     private Number[] source;  
  5.   
  6.     public BubbleSort(Number[] source) {  
  7.         this.source = source;  
  8.     }  
  9.   
  10.     public Number[] doSort() {  
  11.         for (int i = source.length - 1; i > 1; i--) {  
  12.             for (int j = 0; j < i; j++)  
  13.                 if (source[j].doubleValue() > source[j + 1].doubleValue()) {  
  14.                     Number tmp = source[j];  
  15.                     source[j] = source[j + 1];  
  16.                     source[j + 1] = tmp;  
  17.                 }  
  18.         }  
  19.         return source;  
  20.     }  
  21.   
  22.     public static void display(Number[] source) {  
  23.         for (int i = 0; i < source.length; i++) {  
  24.             System.out.println("source[" + i + "] = " + source[i]);  
  25.         }  
  26.     }  
  27.   
  28.     public static void main(String[] args) {  
  29.         Number[] source = {  
  30.                 new Integer(4),   
  31.                 new Double(2.56),   
  32.                 new Float(9.11),   
  33.                 new Long(2),   
  34.                 new Integer("2"),  
  35.                 new Double(5.999999999)};  
  36.         BubbleSort.display(source);  
  37.         BubbleSort bubble = new BubbleSort(source);  
  38.         source = bubble.doSort();  
  39.         BubbleSort.display(source);  
  40.     }  
  41. }  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics