1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| package sort.select;
import java.util.ArrayList; import java.util.Scanner;
public class SelectSort {
public static void main(String[] args) {
Integer[] arr = {8,1,3,2,5,4}; sort(arr); }
private static void sort(Integer[] arr){ int len = arr.length; for (int i = 0; i < len; i++) { int min = i;
for (int j = i+1; j < len; j++) { if(arr[min]>arr[j]) { min = j; } }
if(min!=i) { Integer tmp = arr[i]; arr[i] = arr[min]; arr[min] = tmp; } } for (Integer i:arr ) { System.out.println(i); } }
public static Integer[] scanNum() { Scanner sc = new Scanner(System.in); ArrayList<Integer> list = new ArrayList<>(); System.out.print("输入一串数字,空格分开,以#结束:"); while (!sc.hasNext("#")) { int a = sc.nextInt(); list.add(a); }
return list.toArray(new Integer[0]); } }
|