冒泡排序
升序排列
import java.util.Arrays;
public class ArrayDemo07 {
public static void main(String[] args) {
int[] a = {1,4,5,3,14,12,51};
int[] sort = sort(a);
System.out.println(Arrays.toString(sort));
}
public static int[] sort(int[] array){
int temp = 0;
for (int i = 0; i < array.length-1; i++) {
boolean flag = false;
for (int j = 0; j < array.length-1-i; j++) {
if (array[j+1]<array[j]){
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
flag = true;
}
}
if (flag == false){
break;
}
}
return array;
}
}
控制台打印结果
[1, 3, 4, 5, 12, 14, 51]
进程已结束,退出代码0
(END)
标签:sort,Java,int,static,array,排序,public From: https://www.cnblogs.com/houhuilinblogs/p/17975336