Java冒泡排序、选择排序代码实现(初学者)
程序员文章站
2022-03-29 17:09:28
...
import java.util.Arrays;
public class rank {
public static void main(String[] args) {
// 冒泡排序
int[] pao = { 3, 5, 8, 1, 2, 6, 4, 9, 7 };
System.out.println("冒泡排序:");
for (int j = 0; j < pao.length - 1; j++) {
for (int i = 0; i < pao.length - 1 - j; i++) {
int temp;
if (pao[i] > pao[i + 1]) {
temp = pao[i];
pao[i] = pao[i + 1];
pao[i + 1] = temp;
}
}
System.out.println(Arrays.toString(pao));
}
System.out.println("--------------------------");
// 选择排序
int[] xuanze = { 5, 8, 100, 26, 32, 16, 54, 69, 7 };
int s;
System.out.println("选择排序:");
for (int i = 0; i < xuanze.length; i++) {
int mid = i;
s = xuanze[i];
for (int j = i; j < xuanze.length; j++) {
if (xuanze[j] < s) {
s = xuanze[j];
mid = j;
}
}
xuanze[mid] = xuanze[i];
xuanze[i] = s;
System.out.println(Arrays.toString(xuanze));
}
System.out.println("--------------------------");
}
}
上一篇: 关于清除浮动的四种方法
下一篇: css—清除子元素浮动带给父元素带的影响