冒泡排序

public class BubbleSort 
{
	static int[] bubbleSort(int a[],int n)
	{
		int temp=0;
		for(int i=0;i<n;n--)
			for(int j=i;j<n-1;j++)			
			{
				if(a[j]>a[j+1])
				{
					temp = a[j];
					a[j] = a[j+1];
					a[j+1] = temp;
				}						
			}
	return a;					
	}
	public static void main(String[] args)
	{
		int a[] = new int[10];
		for(int i=0;i<10;i++)
		{
			a[i] =  (int) (Math.random()*10%10);//Math.random()返回一個介於0.0與1.0間的一個隨機數
			System.out.print(a[i]+" ");
		}
		System.out.println();
		a = bubbleSort(a,10);		
		for(int i=0;i<10;i++)
		{			
			System.out.print(a[i]+" ");
		}
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章