1. Select the Sorting method
Find the first small number, put it in the first position, find the second smallest number, and in the second position, find the minimum value for each iteration. Look for a number at a time so that all the values will follow
The order is lined up well. Note that unlike bubbling, values are not exchanged during traversal. The end of a traversal is not exchanged.
Packagecom.sphere.algorith.design;Importjava.util.Arrays; Public classTest { Public Static voidMain (string[] args) {int[] arr = {3,6,1,7,9,-8,2,5}; System.out.println ("Before sorting:" +arrays.tostring (arr)); int[] Sort_arr =Selection_sort (arr); System.out.println ("After sorting:" +arrays.tostring (Sort_arr)); } //Select Sort Method Public Static int[] Selection_sort (int[] Array) { if(Array = =NULL|| Array.Length = = 0) {System.out.println ("The sorted array is empty!" "); return NULL; } //Array Length intLen =Array.Length; //Loop Counter intI, J; intMinValue = 0;//record the minimum value in a traverse intindex = 0;//index value that records the minimum value for(i = 0; i < len-1; i++) {MinValue= Array[i];//assuming that the first element is the smallestindex =i; for(j = i+1; J < Len; J + +) { if(Array[j] <minValue) {MinValue= Array[j];//each time the remaining minimum value and its subscript are obtained. index =J; }} Array[index]= Array[i];//at the end of a traversal, exchange the smallest value with the first elementArray[i] =MinValue; } returnArray; }}
Before sorting: [3, 6, 1, 7, 9,-8, 2, 5]
After sorting: [-8, 1, 2, 3, 5, 6, 7, 9]
Simple numeric sorting algorithm bubble, select, direct insert sort