A binary search implementation. : Sort Search « Collections Data Structure « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Collections Data Structure » Sort SearchScreenshots 
A binary search implementation.
   
/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
 * The contents of this file are subject to the terms of either the GNU
 * General Public License Version 2 only ("GPL") or the Common Development
 * and Distribution License("CDDL") (collectively, the "License").  You
 * may not use this file except in compliance with the License. You can obtain
 * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
 * or glassfish/bootstrap/legal/LICENSE.txt.  See the License for the specific
 * language governing permissions and limitations under the License.
 
 * When distributing the software, include this License Header Notice in each
 * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
 * Sun designates this particular file as subject to the "Classpath" exception
 * as provided by Sun in the GPL Version 2 section of the License file that
 * accompanied this code.  If applicable, add the following below the License
 * Header, with the fields enclosed by brackets [] replaced by your own
 * identifying information: "Portions Copyrighted [year]
 * [name of copyright owner]"
 
 * Contributor(s):
 
 * If you wish your version of this file to be governed by only the CDDL or
 * only the GPL Version 2, indicate your decision by adding "[Contributor]
 * elects to include this software in this distribution under the [CDDL or GPL
 * Version 2] license."  If you don't indicate a single choice of license, a
 * recipient has the option to distribute your version of this file under
 * either the CDDL, the GPL Version 2 or to extend the choice of license to
 * its licensees as provided above.  However, if you add GPL Version 2 code
 * and therefore, elected the GPL Version 2 license, then the option applies
 * only if the new code is made subject to such option by the copyright
 * holder.
 */

// ----------------------------------------------------------------------------
/**
 * A binary search implementation.
 */
public class BinarySearch {

  public int find(final int[] data, final int key) {
    int low = 0, high = data.length - 1;

    while (low <= high) {
      final int i = (low + high>> 1;
      final int v = data[i];

      if (v == key)
        return i; // this line does not get covered unless there is a match
      else if (v < key)
        low = i + 1;
      else
        // v > key
        high = i - 1;
    }

    return -1;
  }

}

   
    
    
  
Related examples in the same category
1. Linear search
2. Animation for quick sort
3. Quick Sort Implementation with median-of-three partitioning and cutoff for small arrays
4. Simple Sort DemoSimple Sort Demo
5. A simple applet class to demonstrate a sort algorithm
6. Sorting an array of StringsSorting an array of Strings
7. Simple version of quick sortSimple version of quick sort
8. Combine Quick Sort Insertion SortCombine Quick Sort Insertion Sort
9. Quick sort with median-of-three partitioningQuick sort with median-of-three partitioning
10. Fast Quick Sort
11. Selection sortSelection sort
12. Insert Sort for objectsInsert Sort for objects
13. Insert sortInsert sort
14. Bubble sortBubble sort
15. Merge sortMerge sort
16. Fast Merge Sort
17. Binary SearchBinary Search
18. Shell sortShell sort
19. Recursive Binary Search Implementation in Java
20. Topological sortingTopological sorting
21. Heap sortHeap sort
22. Sort NumbersSort Numbers
23. A quick sort demonstration algorithmA quick sort demonstration algorithm
24. Performing Binary Search on Java byte Array Example
25. Performing Binary Search on Java char Array Example
26. Performing Binary Search on Java double Array Example
27. Performing Binary Search on Java float Array Example
28. Performing Binary Search on Java int Array Example
29. Performing Binary Search on Java long Array Example
30. Performing Binary Search on Java short Array
31. Sort items of an array
32. Sort an array of objects
33. Sort a String array
34. Sort string array with Collator
35. Binary search routines
36. Customized Sort Test
37. FastQSorts the [l,r] partition (inclusive) of the specfied array of Rows, using the comparator.FastQSorts the [l,r] partition (inclusive) of the specfied array of Rows, using the comparator.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.