1 / 10

The Binary Search Algorithm

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 )”.

dermot
Download Presentation

The Binary Search Algorithm

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The Binary Search Algorithm in Structured Flowchart Form Implemented in Both C/C++ and Java Bary W Pollack Dec. 28, 2001

  2. 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

  3. 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

  4. 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);}

  5. 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; }

  6. 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!

  7. 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!"); }

  8. 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; }

  9. 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!

More Related