Download presentation

Presentation is loading. Please wait.

Published byAlexandrina McDonald Modified about 1 year ago

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

2
Binary Search2 Main Print “Fin!” Return 0 Print “i is at BinarySearch (i, A, LTH )” For i = -1 thru LTH by 1 A [i] = i For i = 0 thru LTH-1 by 1 Print “Demonstrate Binary Search” Main A = array of int LTH = length of A

3
Binary Search3 Return Locn Hi = m-1Lo = m+1 Key < A[m] Key = A[m] Locn = m Break m = (Lo + Hi) / 2 While Hi > Lo Locn = -1 Lo = 0 Hi = LTH - 1 BinarySearch (Key, A, LTH) Binary Search Flowchart

4
Binary Search4 C/C++ Main Routine // // File: BinarySearch.c // // Demonstrate the BinarySearch function // #include 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
Binary Search5 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
Binary Search6 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
Binary Search7 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
Binary Search8 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
Binary Search9 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!

10
Binary Search10

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google