0% found this document useful (0 votes)
71 views10 pages

The Binary Search Algorithm: in Structured Flowchart Form Implemented in Both C/C++ and Java

The document describes the binary search algorithm and provides implementations in C/C++ and Java. It includes: 1) Flowcharts and pseudocode describing the binary search process of dividing a sorted array in half at each step to search for a target value. 2) C/C++ code for a main function that demonstrates binary search on an integer array, and a binarySearch function that implements the algorithm. 3) Java code with a similar demonstration using a BinarySearch class with a main method and binarySearch method. 4) Output showing the binary search locations for sample values in the test arrays.

Uploaded by

hs759
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views10 pages

The Binary Search Algorithm: in Structured Flowchart Form Implemented in Both C/C++ and Java

The document describes the binary search algorithm and provides implementations in C/C++ and Java. It includes: 1) Flowcharts and pseudocode describing the binary search process of dividing a sorted array in half at each step to search for a target value. 2) C/C++ code for a main function that demonstrates binary search on an integer array, and a binarySearch function that implements the algorithm. 3) Java code with a similar demonstration using a BinarySearch class with a main method and binarySearch method. 4) Output showing the binary search locations for sample values in the test arrays.

Uploaded by

hs759
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 10

The Binary Search Algorithm

in Structured Flowchart Form Implemented in Both C/C++ and Java


Bary W Pollack Dec. 28, 2001

Main
A = array of int LTH = length of A

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

Binary Search

Binary Search Flowchart


BinarySearch (Key, A, LTH)

Locn = -1 Lo = 0 Hi = LTH - 1 While Hi > Lo

m = (Lo + Hi) / 2
Locn = m Break Key = A[m]

Key < A[m]

Hi = m-1
Return Locn

Lo = m+1

Binary Search

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

Binary Search

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; }
Binary Search 5

C/C++ Program Output


Demonstrate Binary Search -1 0 1 2 3 4 5 6 7 8 9 10 is is is is is is is is is is is is at at at at at at at at at at at at -1 0 1 2 3 4 5 6 7 8 9 -1

Fin!

Binary Search

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

Binary Search

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

Binary Search

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!

Binary Search

Binary Search

10

You might also like