展开全部
import java.util.Scanner;
public class Test {
public static int cd(){
int i ;
System.out.println("*********************");
System.out.println("*******选择排序******");
System.out.println("***** 1 升序*******");
System.out.println("***** 2 降序*******");
System.out.println("***** 0 退出*******");
System.out.println("*********************");
Scanner sc = new Scanner(System.in);
System.out.print("请输入你的选择:");
i = sc.nextInt();
return i;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入n:");
int n = sc.nextInt();
int[] a = new int[n];
for(int i = 0; i < n; i++){
System.out.print("输入第[ " + (i + 1) +" ]个数 : ");
a[i] = sc.nextInt();
}
int i ;
i = cd();
while(i != 0){
switch (i) {
case 1:
for (i = 0; i < a.length - 1; i++) {
int min = i;
for (int j = i + 1; j < a.length; j++) {
if (a[min] > a[j]) {
min = j;
}
}
if (min != i) {
int temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
System.out.println("升序排序后的结果 : ");
for(int temp : a){
System.out.print(temp + " ");
}
System.out.println();
break;
case 2:
for (i = 0; i < a.length - 1; i++) {
int min = i;
for (int j = i + 1; j < a.length; j++) {
if (a[min] < a[j]) {
min = j;
}
}
if (min != i) {
int temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
System.out.println("降序排序后的结果 : ");
for(int temp : a){
System.out.print(temp + " ");
}
System.out.println();
break;
case 0:
System.out.println("exit!");
break;
}
i = cd();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询