`

Basics - Binary search

 
阅读更多
java 代码
  1. public class BinarySearch {   
  2.        
  3.     public static int search(int element, int[] sortedArray) {   
  4.         int length = sortedArray.length;   
  5.         int left = 0;   
  6.         int right = length - 1;   
  7.            
  8.         while (left <= right) {   
  9.             int mid = (left + right) / 2;   
  10.             if (sortedArray[mid]==element) {   
  11.                 return mid;   
  12.             } else if (sortedArray[mid] > element) {   
  13.                 right = mid - 1;   
  14.             } else {   
  15.                 left = mid + 1;   
  16.             }   
  17.         }   
  18.            
  19.         return -1;   
  20.     }   
  21.        
  22.     public static void main(String[] args) {   
  23.         int[] array = {1234567};   
  24.         System.out.println(search(2, array));   
  25.     }   
  26. }  
分享到:
评论

相关推荐

    Data.Structures.and.Algorithms.USING.C

    17. Binary Search 18. Interpolation Search 19. Hash Table SORTING TECHNIQUES 20. Sorting Algorithm 21. Bubble Sort Algorithm 22. Insertion Sort 23. Selection Sort 24. Merge Sort Algorithm 25. Shell ...

    programming:编程实践

    Recursion -&gt; Linked list -&gt; Stack -&gt; Queue -&gt; Two pointers -&gt; Sliding-window -&gt; hashing -&gt; sorting -&gt; binary search -&gt; trees -&gt; BST -&gt; Heaps -&gt; Graph basics -&gt; BFS -&gt; DFS -&gt; backtracking -&gt; greedy -&gt; ...

    算法导论-introduction to algrithms 3rd edition

    15.3 Elements of dynamic programming 378 15.4 Longest common subsequence 390 15.5 Optimal binary search trees 397 16 Greedy Algorithms 414 16.1 An activity-selection problem 16.2 Elements of the ...

    算法导论_英文第三版

    12.4 Randomly built binary search trees 299 13 Red-Black Trees 308 13.1 Properties of red-black trees 308 13.2 Rotations 312 13.3 Insertion 315 13.4 Deletion 323 14 Augmenting Data Structures 339 ...

    Introduction to Algorithms, 3rd edtion

    12.4 Randomly built binary search trees 299 13 Red-Black Trees 308 13.1 Properties of red-black trees 308 13.2 Rotations 312 13.3 Insertion 315 13.4 Deletion 323 14 Augmenting Data Structures 339 14.1...

    算法导论 第三版 英文原版 高清文字版

    12.4 Randomly built binary search trees 299 13 Red-Black Trees 308 13.1 Properties of red-black trees 308 13.2 Rotations 312 13.3 Insertion 315 13.4 Deletion 323 14 Augmenting Data Structures 339 ...

    算法导论--Introduction.to.Algorithms

    12.4 Randomly built binary search trees 299 13 Red-Black Trees 308 13.1 Properties of red-black trees 308 13.2 Rotations 312 13.3 Insertion 315 13.4 Deletion 323 14 Augmenting Data Structures 339 14.1...

    Learning.JavaScript.Data.Structures.and.Algorithms.1783554878

    Finally, we will round off by learning how to differentiate between various searching and sorting algorithms such as sequential search, binary search, quick sort, bubble sort, and so on, and how to ...

    Java 9 Data Structures and Algorithms

    Use binary search, sorting, and efficient sorting—quicksort and merge sort Work with the important concept of trees and list all nodes of the tree, traversal of tree, search trees, and balanced ...

    Practical C++ Programming C++编程实践

    Debugging and Optimization Code Reviews Serial Debugging Going Through the Output Interactive Debuggers Debugging a Binary Search Interactive Debugging Tips and Tricks Runtime Errors Optimization How...

    Swift Functional Programming - Second Edition

    Make use of functional data structures such as semigroup, monoid, binary search tree, linked list, stack, and lazy list Understand the importance of immutability, copy constructors, and lenses Develop...

    Packt.Swift.Functional.Programming.2nd.Edition.2017

    Make use of functional data structures such as semigroup, monoid, binary search tree, linked list, stack, and lazy list Understand the importance of immutability, copy constructors, and lenses Develop...

    Swift 3 Functional Programming 【2016】

    Functional data structures such as Semigroup, Monoid, Binary Search Tree, Linked List, Stack, and Lazy List Immutability, copy constructors, and lenses Combining FP paradigms with OOP, FRP, and POP in...

    javacv-platform-1.3.3-src

    Follow the instructions on this page: http://developer.android.com/training/basics/firstapp/ Copy all the JAR files into the app/libs subdirectory. Navigate to File &gt; Project Structure &gt; app &gt; ...

    VB编程资源大全(英文源码 网络)

    Users must have msxml.dll version 2.0 for binary compatibility.&lt;END&gt;&lt;br&gt;46 , CustEditXML.zip Complete VB application that retrieves customer information from an XML script, allows you to make ...

    Competitive Programmer's Handbook Antti Laaksonen

    3.3 Binary search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 4 Data structures 35 4.1 Dynamic arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 4.2 Set ...

    bochs user manual 单html文件+pdf 英文原版

    5.2. Search order for the configuration file 5.3. The configuration interface 'textconfig' 5.3.1. The start menu 5.3.2. The Bochs headerbar 5.3.3. The runtime configuration 5.4. Pre-defined CPU models...

    Probabilistic Robotics .pdf

    I Basics 1 1 Introduction 3 1.1 Uncertainty in Robotics 3 1.2 Probabilistic Robotics 4 1.3 Implications 9 1.4 Road Map 10 1.5 Teaching Probabilistic Robotics 11 1.6 Bibliographical Remarks 11 2 ...

Global site tag (gtag.js) - Google Analytics