[Basic idea]
In the set of numbers to sort. Select the smallest number to exchange with the first position, and then in the remaining number to find the smallest and second position of the number of exchanges, so loop to the penultimate number and the last number of the comparison so far.
Algorithm key: Find the smallest number. Use a variable to remember its subscript.
[Java Implementation]
public class Simpleselectionsort {public static void main (string[] args) {int[] arr = {9, 1, 5, 8, 3, 7, 4, 6, 2}; System.out.println ("Before sorting:"); for (int i = 0; i < arr.length; i++) {System.out.print (Arr[i] + "");} Simple selection of sort simpleselectionsort (arr); System.out.println (); System.out.println ("After sorting:"); for (int i = 0; i < arr.length; i++) {System.out.print (Arr[i] + "");}} /** * Simple selection sort */private static void Simpleselectionsort (int[] arr) {for (int i = 0; i < arr.length; i++) { int min = i; Defines the current subscript as the lowest value subscript for (int j = i + 1; j < Arr.length; J + +) {if (Arr[min] > ARR[J]) {//Assuming there is a keyword less than the current minimum value, assign the subscript of this keyword to min min = j; }} if (i! = min) {//If min is not equal to I, the description finds the minimum value. Swap swap (arr, I, min); }}}/** * Element exchange position */private static void swap (int[] arr, int i, int min) {int tmp = Arr[min]; Arr[min] = Arr[i]; Arr[i] = tmp;}}[Algorithmic features]
Complexity of Time: O (n^2)
The most important feature of this algorithm is that the number of mobile data exchanged is very small, which saves the corresponding time. Analyzing its time complexity you will find that, regardless of the best and worst case, it is the same number of times.
Simple selection of the performance of the sorting is better than the bubble sort.
Java sorting Algorithm (ii): Simple selection sorting