Java implementation
PackageCommon; Public classsimplearithmetic {/*** Select sort * Input shaping array: A[n] "4, 5, 3, 7" * 1. The array value of the array number I (I belongs to [0, N-2]) is a[i], i.e. the first cycle * 2. Assuming a[i] is an array a[k] (k belongs to [i,n- 1]), that is, the initialization min =i * 3 is performed at the minimum value of a[min]. The array value of array number m (m belongs to [i+1,n-1]) is a[m], which is the second cycle * 4. If A[M] < a[min], min = m; min always records the minimum value in A[k] * 5. When min! = I, that is, the second step of the Assumption A[i] is a[m] The minimum value is not true, you need to swap the value of a[min] with A[i] *@paramarr Shaping Array*/ Public Static voidSelectsort (int[] arr) { intn =arr.length; intMin = 0; intTMP = 0; for(inti = 0; i < n-1; i++) {min=i; for(intm = i + 1; M < n; m++){ if(Arr[m]<arr[min]) min =m; } if(I! =min) {tmp=Arr[i]; Arr[i]=Arr[min]; Arr[min]=tmp; }//End of If}//End of for } Public Static voidMain (string[] args) {//TODO Auto-generated method stubs int[] arr = {12,23,9,24,15,3,18}; Simplearithmetic.selectsort (arr); for(inti = 0; i < arr.length; i++) {System.out.print (Arr[i]+" "); } //Output 3 9 }}
Python implementation
Python 2.7.9 (default, Dec, 12:24:55) [MSC v.1500 32bit (Intel)] on Win32type"Copyright","credits" or "license ()" forMore information.>>>classsimplearithmetic:defSelectsort (Self,arr): V_len=Len (arr) v_index_i=0 whileV_index_i! = v_len-1: I=Arr[v_index_i] V_min=I forMinchArr[v_index_i+1: V_len]:ifM <V_min:v_min=mifI! =v_min:v_tmp=Arr.index (v_min) arr[v_index_i]=v_min Arr[v_tmp]=I v_index_i= v_index_i + 1>>> sa =simplearithmetic ()>>> arr = [12,23,9,24,15,3,18]>>>Sa.selectsort (arr)>>>arr[3, 9, 12, 15, 18, 23, 24]>>>
Choosing a sorting algorithm Java and Python implementations