The choice of sorting is also a relatively simple sort method, the principle is also relatively easy to understand, it is the same as the bubble sort, but the choice of the number of exchanges less than the bubble sort. Bubble sort is after each comparison, if the comparison of the two elements in the order of the order to be sorted in reverse, then to be exchanged, and the selection of the sequence is recorded only the smallest element of the subscript, after the end of all comparisons, the smallest element with the unsorted part of the sequence of the first element of the exchange, This reduces the number of exchanges and improves the sorting efficiency.
Take a look at the implementation code:
public class Choicesort {public static void _choicesort (integer[] a) {if (a = = NULL | | a.length <= 0) {return;} for (int i = 0; i < a.length-1; i++) {//Why here-1? Think about int min = i; /* Define the current subscript as the minimum subscript */for (int j = i + 1; j < A.length; J + +) {if (A[min] > A[j]) {/* If there is a keyword less than the current minimum value */min = j;/* This key The subscript of the word assigns a value to min */}}if (i! = min) {/* if min is not equal to I, the description finds the minimum value, interchange */int tmp = a[min];a[min] = a[i];a[i] = tmp;}}}
To write a test method:
public static void Main (string[] args) {integer[] list={49,38,65,97,76,13,27,14,10}; Select Sort Choicesort._choicesort (list); for (int i=0;i<list.length;i++) {System.out.print (list[i]+ ""); } System.out.println (); }
Output Result:
Favorite friends pay attention to me! Thanks for your support!
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java implementation algorithm--select sort