冒泡排序算法优化之路
2018-06-08 本文已影响0人
wanghonglin
先写个冒泡排序,慢慢对排序进行优化!
/**
-
@ClassName: SortTest
-
@Description:
-
@Author: WangHongLin
-
@Date: 2018-06-08 下午 8:56
*/
public class SortTest {/**
-
冒泡排序
*/
@org.junit.Test
public void test() {
int[] data = {900,100,2,50,101,30,25,15,10,17,1};
int size = data.length;
int compareCount = size - 1;
int f, g, l = 0;
int length = 0;
for(int i = 0; i < size; i++) {
length = compareCount - i;
for(int k = 0; k < length; k++) {
l = k + 1;
g = data[k];
f = data[l];
if (g > f) {
data[k] = f;
data[l] = g;
}
}
}print(data);
}
private void print(int[] data) {
Arrays.stream(data).forEach(value -> {
System.out.println(value);
});
}
} -