冒泡排序

冒泡排序

1.原理:比较两个相邻的元素,将值大的元素交换到右边

2.思路:依次比较相邻的两个数,将比较小的数放在前面,比较大的数放在后面。

public static void main(String[] args)
	{
		int[] a={2,9,1,6,3};
		int temp=a[0];
		for(int i=0;i<5;i++)
		{
			for(int j=0;j<4;j++)
			{
				if(a[j]>a[j+1])
				{
					temp=a[j];
					a[j]=a[j+1];
					a[j+1]=temp;
				}
			}
		}
		for(int i=0;i<5;i++)
		{
			System.out.print(a[i]+" ");
		}
	}
原文地址:https://www.cnblogs.com/junfblog/p/14567964.html