算法学习(java实现)

不管是在面试中,还是在日常中,算法的重要性不可言喻,接下来将持续更新算法:

第1个:冒泡排序:

           冒泡排序,大一的C语言中接触的第一个算法。主要的思路是“把大的数往前推”。时间复杂度:O(n2)

           demo如下”

       

/**
 * 经典的冒泡排序
 * */
public class FirstSort {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		int[] a = new int[] { 1, 4, 23, 7, 8, 9, 1, 20 };
		int temp;
		for (int i = 0; i < a.length; i++) {
			System.out.print(a[i] + " ");
		}

		System.out.println();

		for (int i = 0; i < a.length; i++)
			for (int j = i + 1; j < a.length ; j++) {
				if (a[i] > a[j]) {
					temp = a[i];
					a[i] = a[j];
					a[j] = temp;
				}
			}

		for (int i = 0; i < a.length; i++) {
			System.out.print(a[i] + " ");
		}
	}

}

  输出如下:

1 4 23 7 8 9 1 20 

1 1 4 7 8 9 20 23

原文地址:https://www.cnblogs.com/qgzhan/p/3020463.html