Site icon C1CTech

Binary Search(Java Implementation)

<p><span style&equals;"color&colon; &num;008000&semi;"><strong>Binary search<&sol;strong><&sol;span> works on sorted arrays&period; Binary search begins by comparing the middle element of the array with the target value&period; If the target value matches the middle element&comma; its position in the array is returned&period; If the target value is less than the middle element&comma; the search continues in the lower half of the array&period; If the target value is greater than the middle element&comma; the search continues in the upper half of the array&period; By doing this&comma; the algorithm eliminates the half in which the target value cannot lie in each iteration&period;<&sol;p>&NewLine;<h4><strong><span style&equals;"color&colon; &num;000080&semi;">Algorithm&colon;<&sol;span><&sol;strong><&sol;h4>&NewLine;<ul>&NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;">step 1 &colon;<&sol;span><&sol;strong> Compare x&lpar;item To Search&rpar; with the middle element&period;<&sol;li>&NewLine;<li><span style&equals;"color&colon; &num;0000ff&semi;"><strong>step 2 &colon; <&sol;strong><&sol;span> If x matches with middle element&comma; return the mid index&period;<&sol;li>&NewLine;<li><span style&equals;"color&colon; &num;0000ff&semi;"><strong>step 3 &colon;<&sol;strong><&sol;span> Else If x is greater than the mid element&comma; then x can only lie in right half subarray after the mid element&period; So we recur for right half&period;<&sol;li>&NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;">step 4 &colon;<&sol;span><&sol;strong> Else &lpar;x is smaller&rpar; recur for the left half&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h4><strong><span style&equals;"color&colon; &num;000080&semi;">Iterative implementation of Binary Search<&sol;span><&sol;strong><&sol;h4>&NewLine;<pre style&equals;"padding-left&colon; 30px&semi;"><strong><code>public class Search &lbrace;&NewLine;&NewLine;<span style&equals;"color&colon; &num;0000ff&semi;">&sol;&sol;return index of item if it is present in arr&lbrack;&rsqb;<&sol;span>&NewLine;<span style&equals;"color&colon; &num;0000ff&semi;">&sol;&sol;else return -1<&sol;span>&NewLine;public int binarySearch&lpar;int arr&lbrack;&rsqb;&comma; int u&comma;int l&comma; int item&rpar; &NewLine; &lbrace;&NewLine; while &lpar;l &lt&semi;&equals; u&rpar; &NewLine; &lbrace; &NewLine; int mid &equals; &lpar;l &plus; u&rpar; &sol; 2&semi;&NewLine;<span style&equals;"color&colon; &num;0000ff&semi;"> &sol;&sol;check if item is present at middle<&sol;span>&NewLine; if &lpar;arr&lbrack;mid&rsqb; &equals;&equals; item&rpar; &NewLine; return mid&semi; &NewLine;<span style&equals;"color&colon; &num;0000ff&semi;"> &sol;&sol;if item is smaller ignore right half<&sol;span>&NewLine; else if &lpar;arr&lbrack;mid&rsqb; &gt&semi; item&rpar;&NewLine; u &equals; mid - 1&semi;&NewLine; else&NewLine;<span style&equals;"color&colon; &num;0000ff&semi;"> &sol;&sol;if item is greater ignore left half&NewLine;<&sol;span> l &equals; mid &plus; 1&semi;&NewLine; &rcub;&NewLine;<span style&equals;"color&colon; &num;0000ff&semi;"> &sol;&sol;return -1 if element is not found&NewLine;<&sol;span> return &lpar;-1&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine;public static void main&lpar;String&lbrack;&rsqb; strr&rpar; &NewLine;&lbrace;&NewLine;<span style&equals;"color&colon; &num;0000ff&semi;">&sol;&sol;l for lower <&sol;span>&NewLine;<span style&equals;"color&colon; &num;0000ff&semi;">&sol;&sol;u for upper <&sol;span>&NewLine;int u&comma; l&comma; itemToSearch&semi;&NewLine;int arr&lbrack;&rsqb; &equals; &lbrace;1&comma; 2&comma; 10&comma; 13&comma; 17&comma; 29&comma; 33&comma; 42&comma; 49&comma; 98&comma; 100&rcub;&semi;&NewLine;u &equals; arr&period;length - 1&semi;&NewLine;l &equals; 0&semi;&NewLine;itemToSearch &equals; 10&semi;&NewLine;int index &equals; new Search&lpar;&rpar;&period;binarySearch&lpar;arr&comma;u&comma;l&comma;itemToSearch&rpar;&semi;&NewLine;if &lpar;index&equals;&equals;-1&rpar;&NewLine;System&period;out&period;println&lpar;"item not exist"&rpar;&semi;&NewLine;else&NewLine;System&period;out&period;println&lpar;"Item"&plus;" " &plus;itemToSearch&plus;" " &plus; "found at index &colon;" &plus; index &plus; " and " &plus;"position &colon;" &plus; &lpar;index&plus;1&rpar;&rpar;&semi;&NewLine;&rcub;&NewLine;&rcub;  <&sol;code><&sol;strong><&sol;pre>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Output&colon;<&sol;strong><&sol;span><&sol;h4>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">Item 10 is found at index &colon;2 and position &colon;3<&sol;span><&sol;strong><&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;<h4><strong><span style&equals;"color&colon; &num;000080&semi;"> Recursive implementation of Binary Search<&sol;span><&sol;strong><&sol;h4>&NewLine;<pre style&equals;"padding-left&colon; 30px&semi;"><strong><code>public class Search &lbrace;&NewLine; &NewLine;public int binarySearch&lpar;int arr&lbrack;&rsqb;&comma; int l&comma;int u&comma; int item&rpar; &NewLine; &lbrace; &NewLine; if &lpar;u&gt&semi;&equals;l&rpar;&NewLine; &lbrace; &NewLine; int mid &equals; l &plus; &lpar;u - l&rpar;&sol;2&semi; &NewLine; if &lpar;arr&lbrack;mid&rsqb; &equals;&equals; item&rpar;&lbrace; &NewLine; return mid&semi; &NewLine; &rcub; &NewLine; if &lpar;arr&lbrack;mid&rsqb; &gt&semi; item&rpar;&lbrace; &NewLine;<span style&equals;"color&colon; &num;0000ff&semi;"> &sol;&sol;search in left subarray<&sol;span>&NewLine; return binarySearch&lpar;arr&comma; l&comma; mid-1&comma; item&rpar;&semi; &NewLine; &rcub;&NewLine; else&lbrace; &NewLine;<span style&equals;"color&colon; &num;0000ff&semi;"> &sol;&sol;search in right subarray<&sol;span> &NewLine; return binarySearch&lpar;arr&comma; mid&plus;1&comma; u&comma; item&rpar;&semi;&NewLine; &rcub; &NewLine; &rcub; &NewLine; return -1&semi; &NewLine; &rcub;&NewLine; &NewLine;public static void main&lpar;String args&lbrack;&rsqb;&rpar;&lbrace; &NewLine;<span style&equals;"color&colon; &num;0000ff&semi;">&sol;&sol;l for lower <&sol;span>&NewLine;<span style&equals;"color&colon; &num;0000ff&semi;">&sol;&sol;u for upper <&sol;span>&NewLine;int u&comma; l&comma; itemToSearch&semi;&NewLine;int arr&lbrack;&rsqb; &equals; &lbrace;1&comma; 2&comma; 10&comma; 13&comma; 17&comma; 29&comma; 33&comma; 42&comma; 49&comma; 98&comma; 100&rcub;&semi;&NewLine;u &equals; arr&period;length - 1&semi;&NewLine;l &equals; 0&semi;&NewLine;itemToSearch &equals; 10&semi;&NewLine;int index &equals; new Search&lpar;&rpar;&period;binarySearch&lpar;arr&comma; l&comma; u&comma; itemToSearch&rpar;&semi; &NewLine;if &lpar;index&equals;&equals;-1&rpar;&NewLine;System&period;out&period;println&lpar;"item not exist"&rpar;&semi;&NewLine;else&NewLine;System&period;out&period;println&lpar;"Item"&plus;" " &plus;itemToSearch&plus;" " &plus; "found at index &colon;" &plus; index &plus; " and " &plus;"position &colon;" &plus; &lpar;index&plus;1&rpar;&rpar;&semi;&NewLine;&rcub;&NewLine;&rcub;<&sol;code><&sol;strong><&sol;pre>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Output&colon;<&sol;strong><&sol;span><&sol;h4>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">Item 10 is found at index &colon;2 and position &colon;3<&sol;span><&sol;strong><&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Time complexity<&sol;strong><&sol;span><&sol;h4>&NewLine;<p>As we dispose off one part of the search case during every step of binary search&comma; and perform the search operation on the other half&comma; this results in a worst case time complexity of O&lpar;Log n&rpar; &comma;where n is the no of elements in the array&period;&NewLine;

Exit mobile version