- package suanfa;
-
- //选择排序
- public class Test1 {
- public static void main(String[] args) {
- int[] arr = new int[] { 9, 2, 3, 4, 0, 8, 6, 7 };
- test(arr);
-
- for (int i = 0; i < arr.length; i++) {
- System.out.print(arr[i]);
- }
-
- }
-
- public static void test(int[] arr) {
- for (int i = 0; i < arr.length; i++) {
- int positionOfMin = i;
-
- for (int j = i; j < arr.length; j++) {
- // 找出这一截的最小值所在的位置
- if (arr[j] < arr[i]) {
- positionOfMin = j;
- swap(arr, positionOfMin, i);
- }
- }
-
- for (int s = 0; s < arr.length; s++) {
- System.out.print(arr[s]);
- }
- System.out.println("-------------------");
- }
-
- }
-
- public static void swap(int[] arr, int positionOfMin, int i) {
- int temp = arr[i];
- arr[i] = arr[positionOfMin];
- arr[positionOfMin] = temp;
- }
- }