This algorithm, I believe we all understand, but not really manual write again, always feel no strength. This is not, manual write again is a different effect appeared.
To the left binary, or to the right to go relatively simple, in fact, the two algorithms are most critical: exit condition min > Max and the next binary when the subscript or superscript position to +1 or 1
/// <summary> ///implementation of a recursive pure algorithm/// </summary> /// <param name= "arrlist" ></param> /// <param name= "min" ></param> /// <param name= "Max" ></param> /// <param name= "des" ></param> /// <returns>index of Hit target</returns> Public intRecursionsearch (int[] arrlist,intMinintMaxintdes) { if (min > Max) return-1; varMidindex = (min + max)/2; varMidvalue =Arrlist[midindex]; if(Midvalue = = des)returnMidindex; returnMidvalue < des? Recursionsearch (Arrlist,Midindex + 1, Max, des): Recursionsearch (arrlist, Min,midIndex-1, des); } /// <summary> ///Pure algorithm implementation of common algorithm/// </summary> /// <param name= "arrlist" ></param> /// <param name= "des" ></param> /// <returns></returns> Public intCommonsearch (int[] arrlist,intdes) { varMin =0; varmax = Arrlist.length-1; While (min < Max) { varMid = (max + min)/2; varMidvalue =Arrlist[mid]; if(Midvalue = = des)returnmid; if(Midvalue < des)min = mid + 1; //Search Right if(Midvalue > Des)max = Mid-1;//Search left } return-1; }
C # Implementation of the binary algorithm-recursive and non-recursive