Java排序算法之冒泡排序

阅读更多
冒泡排序算法的基本思想是:首先将第n-1个记录的关键字和第n个记录的关键字进行比较,若为“逆序”(即L.r[n].key
    以下是冒泡排序的JAVA代码实现:

package com.fit.bubble.sort;

public class BubbleSort {

	/**
	 * 首先将第n-1个记录的关键字和第n个记录的关键字进行比较,若为“逆序”(即L.r[n].key array[j + 1]) {
					temp = array[j + 1];
					array[j + 1] = array[j];
					array[j] = temp;
				}
			}
			//System.out.println("-----------------------");
			print(array);
		}

	}

	public static void print(int[] array) {
		StringBuffer sb = new StringBuffer("[");
		for (int i = 0; i < array.length; i++) {
			if (i < array.length - 1) {
				sb.append(array[i]).append(",");
			} else {
				sb.append(array[i]);
			}
		}

		sb.append("]");

		System.out.println(sb);
	}

	public static void main(String[] args) {
		int[] array = new int[] { 54, 3, 11, 34, 12, 8, 19 };

		print(array);

		System.out.println("=====================");

		sort(array);

		print(array);
	}
}

运行结果如下:
[54,3,11,34,12,8,19]
=====================
[3,11,34,12,8,19,54]
[3,11,12,8,19,34,54]
[3,11,8,12,19,34,54]
[3,11,8,12,19,34,54]
[3,11,8,12,19,34,54]
[3,11,8,12,19,34,54]
[3,11,8,12,19,34,54]
[3,11,8,12,19,34,54]

你可能感兴趣的:(java,算法,排序,冒泡排序)