100 likes | 177 Views
The Binary Search Algorithm. in Structured Flowchart Form Implemented in Both C/C++ and Java Bary W Pollack Dec. 28, 2001. Main. Main. Print “Demonstrate Binary Search”. For i = 0 thru LTH-1 by 1. A [i] = i. For i = -1 thru LTH by 1. Print “i is at BinarySearch (i, A, LTH )”.
E N D
The Binary Search Algorithm in Structured Flowchart Form Implemented in Both C/C++ and Java Bary W Pollack Dec. 28, 2001
Main Main Print “Demonstrate Binary Search” For i = 0 thru LTH-1 by 1 A [i] = i For i = -1 thru LTH by 1 Print “i is at BinarySearch (i, A, LTH )” Print “Fin!” Return 0 A = array of int LTH = length of A
Binary Search Flowchart BinarySearch (Key, A, LTH) Locn = -1 Lo = 0 Hi = LTH - 1 While Hi > Lo m = (Lo + Hi) / 2 Key = A[m] Locn = m Break Key < A[m] Hi = m-1 Lo = m+1 Return Locn
C/C++ Main Routine //---------------------------------------// File: BinarySearch.c//---------------------------------------// Demonstrate the BinarySearch function//---------------------------------------#include <stdio.h>int main (void){ const int LTH = 10; int i, nArray [LTH+1]; int BinarySearch (const int v, const int a [], const int nLth); puts ("Demonstrate Binary Search\n"); for (i = 0; i < LTH; i++) nArray[i] = i; for (i = -1; i <= LTH; i++) printf ("%3d is at %d\n", i, BinarySearch (i, nArray, LTH)); puts ("\nFin!"); return (0);}
C/C++ Binary Search Fcn //-------------------------------------------- // Binary Search Algorithm: // Divide the remaining table in half // Check the midpoint; if found, DONE // If not found, consider the first half // or second half, depending... // and do Binary Search on it // If the partition size goes to ONE, the // item is NOT present in the table //-------------------------------------------- int BinarySearch (const int nKey, const int nArray [], const int nLth) { int nLoc = -1, nLow = 0, nHigh = nLth-1; while (nHigh >= nLow) { int m = (nLow + nHigh) / 2; if (nKey == nArray [m]) { nLoc = m; break; } if (nKey < nArray [m]) nHigh = m - 1; else nLow = m + 1; } return nLoc; }
C/C++ Program Output Demonstrate Binary Search -1 is at -1 0 is at 0 1 is at 1 2 is at 2 3 is at 3 4 is at 4 5 is at 5 6 is at 6 7 is at 7 8 is at 8 9 is at 9 10 is at -1 Fin!
Java Public Main Class // File: BinarySearch.java // Demonstrate the binarySearch method public class BinarySearch { BinarySearch(int[] nArray) { for (int i = 0; i < nArray.length; i++) nArray[i] = i; for (int i = -1; i <= nArray.length; i++) System.out.println(i + " is at " + binarySearch(i, nArray)); } public static void main(String[] args) { System.out.println("Demonstrate " + "Binary Search"); System.out.println(); new BinarySearch(new int[10] System.out.println(); System.out.println("Fin!"); }
Java Binary Search Method //-------------------------------------------- // Binary Search Algorithm: // Divide the remaining table in half // Check the midpoint; if found, DONE // If not found, consider the first half // or second half, depending... // and do Binary Search on it // If the partition size goes to ONE, the // item is NOT present in the table //-------------------------------------------- int binarySearch (final int nKey, final int nArray []) { int nLoc=-1, nLow=0, nHigh=nArray.length-1; while (nHigh >= nLow) { int m = (nLow + nHigh) / 2; if (nKey == nArray [m]) { nLoc = m; break; } if (nKey < nArray [m]) nHigh = m - 1; else nLow = m + 1; } return nLoc; }
Java Program Output Demonstrate Binary Search -1 is at -1 0 is at 0 1 is at 1 2 is at 2 3 is at 3 4 is at 4 5 is at 5 6 is at 6 7 is at 7 8 is at 8 9 is at 9 10 is at -1 Fin!