1、升序如下:
package lxyq;
public class Test {
public static void sort(int[] array) // 排序
{
for (int i = 0; i < array.length; i++) {
for (int j = i + 1; j < array.length; j++) {
System.out.println(j+"<<>>>"+i);
if (array[j] < array[i]) {
int temp = array[j];
array[j] = array[i];
array[i] = temp;
}
}
}
}
public static void main(String[] args) {
int[] a = { 1, 6, 2, 0 };
sort(a);
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
}
2、降序如下:
package lxyq;
public class Test {
public static void sort(int[] array) // 排序
{
for (int i = 0; i < array.length; i++) {
for (int j = i + 1; j < array.length; j++) {
System.out.println(j+"<<>>>"+i);
if (array[j] > array[i]) {
int temp = array[j];
array[j] = array[i];
array[i] = temp;
}
}
}
}
public static void main(String[] args) {
int[] a = { 1, 6, 2, 0 };
sort(a);
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
}