最简单的冒泡排序
代码语言:javascript
复制public class MaoPao {
public static void main(String[] args) {
Integer[] datas = {2,4,5,3,1,8,1,4,3};
maoPao(datas);
System.out.println(Arrays.toString(datas));
//[1, 1, 2, 3, 3, 4, 4, 5, 8]
}
public static void maoPao(Integer[] datas){
for(int i = 0; i < datas.length; i ){
for(int j = 0; j < (datas.length - i - 1); j ){
if(datas[j] > datas[j 1]){
swap(datas, j, j 1);
}
}
}
}
private static void swap(Integer[] datas, int s, int d){
int tmp = datas[s];
datas[s] = datas[d];
datas[d] = tmp;
}
}