Animated demonstration of Selection Sort

1. Sorting rules

1.1 One sentence summary selection sort

Starting from the first number in the array, each number in the array has to be compared with all subsequent numbers, and each cycle traverses the current minimum value and places it in the minimum position within the current cycle range. After the N - 1th , the sorting is complete. N = 数组长度 - 1.

1.2 Sorting methods and rules

Assume that the array arr = [1, 4, 7, 3, 5, 8, 9, 2, 10, 6]is sorted, the length of the array is arr.length, and i is the subscript of the array.

  1. Suppose we use red to mark the minimum value,
  2. Assuming that the i = 0th element in the array (the index subscript is 0) is the minimum value of the entire array, we will mark it in red for the time being.
  3. In order to verify that the i = 0th (index subscript 0) element is the minimum value, he needs to compare it with every element in the array.
  4. During the traversal process, if a smaller value is found, the new minimum value is temporarily marked red before traversing the entire array.
  5. Continue to traverse and repeat the previous step until the end of the loop, the number at the current red position is the minimum value.
  6. Swap the minimum value and the current first number position, because the number in the red position is smaller. '
  7. At this point we have completed a cycle, the smallest number in the entire array has been found, and we mark it orange.
  8. Next, we start the second cycle to find the second smallest number in the array, because the first smallest number has been found, so our second cycle starts from the number i = 1 (the index subscript is 1), the method is Repeat steps 1-7 for the remaining digits except the first digit.
  9. Repeat this, each cycle, you can determine the minimum value of all elements whose subscripts are after i, that is, the minimum value of the current cycle.
    10. After completing the arr.llength -1 loop traversal, the selection sort is completed.

2. Animation demonstration

insert image description here

  • Orange: the final minimum value of each cycle
  • Red: the temporary minimum value during each cycle, when the green scroll finds a smaller value, update the temporary minimum value
  • Blue: elements not yet sorted

3. Java implementation

public class 选择排序 {
    
    

    public static void main(String[] args) {
    
    
        int[] arr = {
    
    1, 4, 7, 3, 5, 8, 9, 2, 10, 6};
        // 排序前
        printArr(arr);
        // 排序
        selectSort(arr);
        // 排序后
        printArr(arr);
    }

    private static void printArr(int[] arr) {
    
    
        for (int i = 0; i < arr.length; i++) {
    
    
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }

    public static void selectSort(int[] arr) {
    
    
        // 0~ N-1
        // 1~ N-1
        // N-2 ~ N-1
        for (int i = 0; i < arr.length; i++) {
    
    
            int minValIndex = i;
            for (int j = i + 1; j < arr.length; j++) {
    
    
                minValIndex = arr[j] < arr[minValIndex] ? j : minValIndex;
            }
            if (minValIndex != i) {
    
    
                swap(arr, i, minValIndex);
            }
        }
    }

    private static void swap(int[] arr, int i, int j) {
    
    
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}

Guess you like

Origin blog.csdn.net/wlei0618/article/details/128090638