冒泡排序及其优化
冒泡排序(Bubble Sort)也是一种简单直观的排序算法。它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来。走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成。这个算法的名字由来是因为越小的元素会经由交换慢慢”浮”到数列的顶端。
平均时间复杂度 n^2 最坏的时间复杂度 n^2 最好时间复杂度n 空间复杂度1 稳定性 稳
代码:
最简单的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| package com.xiaohu.suanfa.sort;
import java.util.Arrays; import java.util.Scanner;
public class BubbleSort { public static void main(String[] args) { print(); }
private static void print() { int[] array = getArray(); System.out.println("排序之前的数组:" + Arrays.toString(array)); long startTime = System.currentTimeMillis(); int[] sort = sort(array); long endTime = System.currentTimeMillis(); System.out.println("排序之后的数组:" + Arrays.toString(sort)); System.out.println("总共花费:"+(endTime-startTime)+"毫秒"); }
private static int[] getArray() { Scanner sc = new Scanner(System.in); System.out.println("请输入一串数字,以,为 间隔"); String str = sc.nextLine(); String[] strArray = str.split(","); int[] arr = new int[strArray.length]; for (int i = 0; i < strArray.length; i++) { arr[i] = Integer.parseInt(strArray[i]); } return arr; }
private static int[] sort(int[] arr) { int temp; for (int i = 0; i < arr.length-1; i++) { System.out.println("这是第"+(i+1)+"次轮回"); for (int j=0;j<arr.length-1-i;j++) { if (arr[j]>arr[j+1]) { temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } System.out.println("这是第"+(j+1)+"次遍历"+Arrays.toString(arr)); } } return arr; } }
|
简单优化:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| package com.xiaohu.suanfa.sort;
import java.util.Arrays; import java.util.Scanner;
public class BubbleSortPlus {
public static void main(String[] args) { print(); }
private static int[] getArray() { Scanner sc = new Scanner(System.in); System.out.println("请输入一串数字,以,为 间隔"); String str = sc.nextLine(); String[] strArray = str.split(","); int[] arr = new int[strArray.length]; for (int i = 0; i < strArray.length; i++) { arr[i] = Integer.parseInt(strArray[i]); } return arr; }
private static void print() { int[] array = getArray(); System.out.println("排序之前的数组:" + Arrays.toString(array)); long startTime = System.currentTimeMillis(); int[] sort = sort(array); long endTime = System.currentTimeMillis(); System.out.println("排序之后的数组:" + Arrays.toString(sort)); System.out.println("总共花费:"+(endTime-startTime)+"毫秒"); }
private static int[] sort(int[] arr) { int temp; for (int i = 0; i < arr.length-1; i++) { System.out.println("这是第"+(i+1)+"次轮回"); boolean flag = true; for (int j = 0; j < arr.length-1-i; j++) { if (arr[j]>arr[j+1]) { temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; flag = false; } System.out.println("这是第"+(j+1)+"次遍历"+Arrays.toString(arr)); } if (flag) { break; } } return arr; } }
|
再加工:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| package com.xiaohu.suanfa.sort;
import java.util.Arrays; import java.util.Scanner;
public class BubbleSortPluss {
public static void main(String[] args) { print(); }
private static int[] getArray() { Scanner sc = new Scanner(System.in); System.out.println("请输入一串数字,以,为 间隔"); String str = sc.nextLine(); String[] strArray = str.split(","); int[] arr = new int[strArray.length]; for (int i = 0; i < strArray.length; i++) { arr[i] = Integer.parseInt(strArray[i]); } return arr; }
private static void print() { int[] array = getArray(); System.out.println("排序之前的数组:" + Arrays.toString(array)); long startTime = System.currentTimeMillis(); int[] sort = sort(array); long endTime = System.currentTimeMillis(); System.out.println("排序之后的数组:" + Arrays.toString(sort)); System.out.println("总共花费:"+(endTime-startTime)+"毫秒"); }
private static int[] sort(int[] arr) { int temp; int unorderedArray = arr.length-1; int lastIndexSwap = 0; for (int i = 0; i < arr.length-1; i++) { System.out.println("这是第"+(i+1)+"次轮回"); boolean flag = true; for (int j = 0; j < unorderedArray; j++) { if (arr[j]>arr[j+1]) { temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; flag = false; lastIndexSwap = j; } System.out.println("这是第"+(j+1)+"次遍历"+Arrays.toString(arr)); } unorderedArray = lastIndexSwap; if (flag) { break; } } return arr; } }
|