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

Slides:



Advertisements
Similar presentations
Recursion.
Advertisements

More on Recursion More techniques 1. Binary search algorithm Binary searching for a key in an array is similar to looking for a word in dictionary Take.
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
CompSci Searching & Sorting. CompSci Searching & Sorting The Plan  Searching  Sorting  Java Context.
Arrays (II) H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 14, 2011) Washington State University.
Arrays Arrays are data structures consisting of data items of the same type. Arrays are ‘static’ entities, in that they remain the same size once they.
Problem Solving #6: Search & Sort ICS Outline Review of Key Topics Review of Key Topics Problem 1: Recursive Binary Search … Problem 1: Recursive.
June Searching CE : Fundamental Programming Techniques 16 Searching1.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L10 (Chapter 19) Recursion.
Searching Algorithms. Lecture Objectives Learn how to implement the sequential search algorithm Learn how to implement the binary search algorithm To.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
1 Searching Algorithms Overview  What is Searching?  Linear Search and its Implementation.  Brief Analysis of Linear Search.  Binary Search and its.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Searching Arrays Linear search Binary search small arrays
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
CS212: DATASTRUCTURES Lecture 3: Searching 1. Search Algorithms Sequential Search It does not require an ordered list. Binary Search It requires an ordered.
Lecture 4 Loops.
CS 46B: Introduction to Data Structures July 7 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Building Java Programs Chapter 13 Searching reading: 13.3.
Discrete Structures Lecture 11: Algorithms Miss, Yanyan,Ji United International College Thanks to Professor Michael Hvidsten.
Sorting. Why Sort? Put matching elements together –Uniqueness testing –Deleting duplicates –Frequency Counting –Set operations Prioritize Elements Reconstruct.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
F27SA1 Software Development 1 9. Java Programming 8 Greg Michaelson.
Applications of Arrays (Searching and Sorting) and Strings
Lecture 12. Searching Algorithms and its analysis 1.
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
Chapter 7 Recursion Recursive methods Recursion in two- dimensional grids Recursive helper method Analysis of recursive algorithms.
1 Lecture 5: Part 1 Searching Arrays Searching Arrays: Linear Search and Binary Search Search array for a key value Linear search  Compare each.
1 Linear and Binary Search Instructor: Mainak Chaudhuri
SEARCHING (Linear/Binary). Searching Algorithms  method of locating a specific item of information in a larger collection of data.  two popular search.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 9 Searching Arrays.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Searching. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
COMP Recursion, Searching, and Selection Yi Hong June 12, 2015.
Arrays The concept of arrays Using arrays Arrays as arguments Processing an arrays data Multidimensional arrays Sorting data in an array Searching with.
Working with arrays (we will use an array of double as example)
CSC 211 Data Structures Lecture 13
Chapter 14: Searching and Sorting
Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):
Catie Welsh April 20,  Program 4 due Wed, April 27 th by 11:59pm  Final exam, comprehensive ◦ Friday, May 6th, 12pm  No class Friday - Holiday.
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
Chapter 10: Class Vector and String, and Enumeration Types Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
RECURSION Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
1 compares each element of the array with the search key. works well for small arrays or for unsorted arrays works for any table slow can put more commonly.
Searching Arrays Linear search Binary search small arrays
Programming in Java: lecture 10
Recursive Binary Search
Data Structures Array - Code.
Computing Adjusted Quiz Total Score
TO COMPLETE THE FOLLOWING:
Data Structures Array - Code.
Recursive GCD Demo public class Euclid {
The Hanoi Tower Problem
class PrintOnetoTen { public static void main(String args[]) {
COMPUTER 2430 Object Oriented Programming and Data Structures I
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
Binary Search class binarysearch {
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
slides created by Alyssa Harding
Sorting and Searching -- Introduction
slides created by Alyssa Harding
Presentation transcript:

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

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

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

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

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

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!

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

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

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!

Binary Search10