冒泡排序是一种最常用排序算法,时间复杂度和空间浮渣度较高,好处是比较稳定,实现简单。一次比较两个元素,如果它们的顺序错误就把它们交换过来。
两两比较,出现顺序错误的术后交换,指针向下一个移动继续比较,一个数要和所有的数都会比较一遍。
package com.algorithm;
public class AlgorithmTest {
public static void main(String[] arge) {
int[] testArray = { 3, 2, 4, 5, 7, 8 };
input(getBubbleSort(testArray));
}
/**
* 输入:{3,2,4,5,7,8} 输出2,3,4,5,7,8
*
*/
public static int[] getBubbleSort(int[] inputArray) {
for (int i = 0; i < inputArray.length; i++) {
for (int j = 0; j < inputArray.length; j++) {
if (inputArray[i] < inputArray[j]) {
int temp = inputArray[i];
inputArray[i] = inputArray[j];
inputArray[j] = temp;
}
}
}
return inputArray;
}
public static void input(int[] array) {
for (int i = 0; i < array.length; i++) {
if (array.length - 1 == i) {
System.out.print(array[i]);
System.out.println();
} else {
System.out.print(array[i] + ",");
}
}
}
}