Computational algorithm time complexity is faster than bubbling
The core idea of choosing a sorting method is to select the minimum value from r[0] ~r[n-1] for the first time and then swap with r[0], and the second time from r[1] ~r[n-1] to swap with r[1]. The first time is to select the minimum value from r[i-1] ~r[n-1] and exchange the element with the r[i-1]; for an array of n elements, a sequence of ordering codes from small to large is obtained for a maximum of two n-1 times.
PackageCom. PENGRONG.A;Importjava.util.*;/** * * @author pengrong * * * * Public class demoselection{ Public Static void Main(string[] args) {//TODO auto-generated method stub //Randomly generate 10,000 test data intlen=10000;int[] arr =New int[Len]; for(intI=0; i<len; i++) {intT = (int) (Math.random () *10000); Arr[i] =t; } Selection Selection =NewSelection (); Selection. Test (); Selection. Sort (arr); Selection. Test ();/*for (int i=0; i<arr.length; i++) {System.out.println ("arr[" +i+ "]=" +arr[i]); }*/}}/** * @ function: Select the sorting implementation; * The idea of sorting is to put the minimum value in the order of the preceding sequence * @author pengrong * */Class selection{/** * @author Pengrong * @ function: Used to calculate the current system time */ Public void Test() {Calendar cal =calendar.getinstance (); System.out.println (Cal.gettime ()); } Public void Sort(int[] arr) {inttemp =0;//Exchange time to count in the middle value intMin =0;//min value intMinindex =0;index of//minimum value ///Outer loop control number, for an array containing n elements only need to loop n-1 times. for(intj=0; j<arr.length-1; J + +) {//per trip Select Sort By default the first element is the smallestmin = Arr[j]; Minindex = j;//Inner loop find the minimum value of this trip min for(intK =j+1; k<arr.length; k++) {if(Min > Arr[k]) {min = arr[k]; Minindex = k; } }//If the minimum value is found to be inconsistent with the default minimum value, interchange arr[j] and Arr[minindex] values if(Minindex! = j) {temp = Arr[j]; ARR[J] = Arr[minindex]; Arr[minindex] = temp; } } }}
This process realizes the time display of the sorting system, and can be seen when the parameter data is len=10000 in int; Level 10,000 algorithm is very fast, the system time can not see the loss
when int len = 80000; 8s
when int len = 100000; 13s
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The choice sort method of choosing Sort method realizes-java edition