你的位置:首页 > Java教程

[Java教程]简单的选择排序


 1 package com.hp.io; 2  3 import java.util.Scanner; 4  5 public class XuanZe { 6   public static void main(String[] args) { 7     Scanner s = new Scanner(System.in); 8     System.out.println("请输入数组的个数"); 9     int a = s.nextInt();10     int[] b = new int[a];11     System.out.println("输入数组中的元素:");12     for (int i = 0; i < b.length; i++) {   //数组中的元素进行操作后都要遍历13       b[i] = s.nextInt();14     }15     for (int i = 0; i < b.length - 1; i++) {16       for (int j = i + 1; j < b.length; j++) {17         if (b[i] > b[j]) {18           int temp = b[i];19           b[i] = b[j];20           b[j] = temp;21         }22       }23     }24     for (int k = 0; k < b.length; k++) {25       System.out.println(b[k] + " ");26     }27   }28 }

 


 1 package com.hp.io; 2  3 import java.util.Scanner; 4  5 public class MaoPao { 6   public static void main(String[] args) { 7     Scanner s = new Scanner(System.in); 8     System.out.println("请输入数组的个数"); 9     int a = s.nextInt();10     int[] b = new int[a];11     System.out.println("输入数组中的元素:"); //对数组中元素进行遍历12     for (int i = 0; i < b.length; i++) {13       b[i] = s.nextInt();  //数组b的元素就是输入的元素14     }15     for (int i = 0; i < b.length - 1; i++) {16       for (int j = i + 1; j < b.length; j++) {17         if (b[i] > b[j]) {18           int temp = b[i]; 19           b[i] = b[j];20           b[j] = temp;21         }22       }23     }24     for (int k = 0; k < b.length; k++) {  //打印的时候还还要求数组进行遍历25       System.out.println(b[k] + " ");26     }27   }