排序算法(五):冒泡排序

来源:互联网 发布:淘宝下载 编辑:程序博客网 时间:2024/04/30 15:00

思路:在要排序的一组数中,对当前还未排好序的范围内的全部数,自上而下对相邻的两个数依次进行比较和调整,让较大的数往下沉,较小的往上冒。即:每当两相邻的数比较后发现它们的排序与排序要求相反时,就将它们互换。


图解:



代码:

public class BubbleSort {public static void main(String[] args) {int a[] = { 49, 38, 65, 97, 4, 62, 99, 98,25, 53, 51 };int temp = 0;for (int i = 0; i < a.length - 1; i++) {for (int j = 0; j < a.length - 1 - i; j++) {if (a[j] > a[j + 1]) {temp = a[j];a[j] = a[j + 1];a[j + 1] = temp;}}}for (int i = 0; i < a.length; i++) {System.out.println(a[i]);}}}

结果:

4
25
38
49
51
53
62
65
97
98
99





原创粉丝点击