BubbleSort

class BubbleSort 
{
    
public static void main(String[] args) 
    {
        
int[] array = new int[]{ 35,2,8,4,9,-1,7 };

        
for(int num : array)
            System.out.println(num);

        
long timeStart = System.currentTimeMillis();
        
for(int i=0; i<10000000; i++)
            BubbleSort.bubbleSort(array);
        
long timeEnd = System.currentTimeMillis();
        System.out.println(
"time: "+(timeEnd-timeStart));

        
for(int num : array)
            System.out.println(num);

    }
    
static void bubbleSort(int[] array){
        
int temp;
        
int i,j;
        
for(i=1; i<array.length; i++){
            
for(j=0; j<array.length-i; j++){
                
if( array[j]>array[j+1] ){
                    temp 
= array[j];
                    array[j] 
= array[j+1];
                    array[j
+1= temp;
                }
            }
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章