ruby

 

Ruby代码 Ruby实现冒泡排序  _41 Ruby实现冒泡排序  _41_02
  1. #冒泡排序   
  2. def bulle_sort   
  3.   a = [40,12,23,543,53,2,77,34]   
  4.   i =0   
  5.   while (i<a.length)   
  6.     j=i+1   
  7.     while(j<a.length)   
  8.       if a[i]>a[j]   
  9.         temp = a[j]   
  10.         a[j] = a[i]   
  11.         a[i] = temp   
  12.       end  
  13.       j=j+1   
  14.     end  
  15.     i=i+1   
  16.   end  
  17.      
  18. end  

 java

 

Java代码 Ruby实现冒泡排序  _41 Ruby实现冒泡排序  _41_02
  1. public static void main(String[] args) {   
  2.         int [] a = {40,12,23,543,53,2,77,34};   
  3.         int temp = 0;   
  4.         for(int i =0;i<a.length;i++){   
  5.             for(int j=i+1;j<a.length;j++){   
  6.                 if (a[i]> a[j]) {   
  7.                   temp = a[i];   
  8.                   a[i] = a[j];   
  9.                   a[j] = temp;   
  10.                }   
  11.             }   
  12.         }   
  13.            
  14. }