The Binary Search Algorithm - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

The Binary Search Algorithm

Description:

The Binary Search Algorithm in Structured Flowchart Form Implemented in Both C/C++ and Java Bary W Pollack Dec. 28, 2001 C/C++ Main Routine ... – PowerPoint PPT presentation

Number of Views:1812
Avg rating:3.0/5.0
Slides: 11
Provided by: BaryWP
Category:

less

Transcript and Presenter's Notes

Title: The Binary Search Algorithm


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

2
Main
A array of int LTH length of A

3
Binary Search Flowchart

4
C/C Main Routine
  • //---------------------------------------//
    File BinarySearch.c//------------------------
    ---------------// Demonstrate the BinarySearch
    function//---------------------------------------
    include ltstdio.hgtint main (void) const
    int LTH 10 int i, nArray LTH1 int
    BinarySearch (const int v,
  • const int a ,
  • const int nLth)
    puts ("Demonstrate Binary Search\n")
  • for (i 0 i lt LTH i) nArrayi
    i for (i -1 i lt 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 gt nLow)
  • int m (nLow nHigh) / 2
  • if (nKey nArray m)

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 lt nArray.length i)
  • nArrayi i
  • for (int i -1 i lt 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 int10
  • System.out.println()

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, nLow0, nHighnArray.length-1
  • while (nHigh gt nLow)
  • int m (nLow nHigh) / 2
  • if (nKey nArray m)
  • nLoc m break
  • if (nKey lt nArray m)
  • nHigh m - 1

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!

10
Write a Comment
User Comments (0)
About PowerShow.com