赞
踩
给定一数组,其大小为8个元素,数组内的数据无序。
public class bubbleSort {
public static void main(String[] args) {
int[] test = { 6, 3, 5, 7, 0, 4, 1, 2 };
for (int i = 0; i < test.length - 1; i++) {
for (int j = 0; j < test.length - i - 1; j++) {
if (test[j] > test[j + 1]) {
int temp = test[j];
test[j] = test[j + 1];
test[j + 1] = temp;
}
}
}
for (int k = 0; k < test.length; k++) {
System.out.println(test[k]);
}
}
}
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
public class selectSort {
public static void main(String[] args) {
int[] test = { 6, 3, 5, 7, 0, 4, 1, 2 };
for (int i = 0; i < test.length; i++) {
int min = i;
for (int j = i + 1; j < test.length; j++) {
if (test[min] > test[j]) {
min = j;
}
}
if (min != i) {
int temp = test[i];
test[i] = test[min];
test[min] = temp;
}
}
for (int k = 0; k < test.length; k++) {
System.out.println(test[k]);
}
}
}
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
public class insertSort {
public static void main(String[] args) {
int[] test = { 6, 3, 5, 7, 0, 4, 1, 2 };
for (int i = 0; i < test.length; i++) {
for (int j = i; j > 0; j--) {
if (test[j] < test[j - 1]) {
int temp = test[j];
test[j] = test[j - 1];
test[j - 1] = temp;
} else {
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。