Algorithms - Selection Sort

What is the selection sort

Select sort is a sorting algorithm, the time complexity can be written as a simple O(n x n), smart but not very fast algorithm

The general idea: to iterate, taken through the array each time a minimum value into the result array, the array volume reduction while removing iterate minimum value, continue traversing

That is, the minimum value in the result array place -> volume reduction to a minimum -> repeat step

Select the sort of step

Given the array

The results of a long array to create the original array, etc.

Using the two variables and a minimum value Min array coordinates are recorded

Take the first original value compared to other elements in the array, if the compared value is less than the current minimum is the minimum current value is replaced, otherwise continue traversing

Returns the current traversing the complete array index minimum value, the minimum value of the original array into a first result of the array, the first pass to give 2

Next, the minimum necessary to remove a given array, narrow array, the second pass to give 3

Each time through the array after narrowing, find the minimum value into the result array, reduce shrink over the last array again until the resulting array to fill

Finally get the results array

Java implementation

package com.github.hellxz.grokkingalgorithms.selectionsort;

/**
 * 选择排序
 */
public class SelectionSort {

    public static int[] selectionSort(int[] arr) {
        //创建结果数组
        int[] solution = new int[arr.length];
        for (int i = 0; i < solution.length; i++) {
            //获得当前arr最小值下标
            int smallestIndex = findSmallestIndex(arr);
            //将当前arr最小值放到结果数组
            solution[i] = arr[smallestIndex];
            //arr缩容,去除最小值
            arr = newArrayWithoutLastSmallest(arr, smallestIndex);
        }
        return solution;
    }

    /**
     * 返回去掉给定值的新数组
     */
    private static int[] newArrayWithoutLastSmallest(int[] arr, int lastSmallestIndex) {
        int[] newArr = new int[arr.length - 1];
        for (int i = 0; i < arr.length; i++) {
            if (i < lastSmallestIndex) {
                newArr[i] = arr[i];
            } else if(i > lastSmallestIndex) {
                newArr[i-1] = arr[i];
            }
        }
        return newArr;
    }

    /**
     * 查找给定数组最小值下标
     */
    private static int findSmallestIndex(int[] arr) {
        int smallest = arr[0];
        int smallestIndex = 0;
        for (int i = 0; i < arr.length; i++) {
            if (smallest > arr[i]) {
                smallest = arr[i];
                smallestIndex = i;
            }
        }
        return smallestIndex;
    }

    public static void main(String[] args) {
        int[] arr = {8, 6, 7, 5, 3, 2, 4};
        int[] sortedArr = selectionSort(arr);
        for (int i = 0; i < sortedArr.length; i++) {
            System.out.print(sortedArr[i]);
        }
    }
}

Guess you like

Origin www.cnblogs.com/hellxz/p/12244349.html