Defines the array length variable $len, using the count () function, parameter: array
For loop Array, condition: From the second start, iterate through the array, inside the loop
Define temporary variable $temp, assign current element
For loop Array, condition: Iterates over all elements in front of the current element
Determines the size of the current element and the elements in front of it, using temporary variables, converting variables
PHP Version:
<?PHP$arr=Array(2,3,4,1,5);functionInsert_sort ($arr){ $len=Count($arr); for($i= 1;$i<$len;$i++){ $temp=$arr[$i]; for($j=$i-1;$j>=0;$j--){ if($temp<$arr[$j]){ $arr[$j+1]=$arr[$j]; $arr[$j]=$temp; }Else{ Break; } } } return $arr;}$arr=insert_sort ($arr);Print_r($arr);
Java Edition:
PackageCom.tsh.mthread; Public classHome { Public Static voidMain (string[] args) {int[] arr=New int[]{2,3,4,1,5}; Arr=Insert_sort (arr); for(intX:arr) {System.out.println (x); } } /*** Insert Sort *@paramarr *@return */ Public Static int[] Insert_sort (int[] arr) { intlen=arr.length; for(inti=1;i<len;i++){ inttemp=Arr[i]; for(intj=i-1;j>=0;j--){ if(temp<Arr[j]) {Arr[j+1]=Arr[j]; ARR[J]=temp; }Else{ Break; } } } returnarr; }}
[PHP] Watch blog Learning insert sort