`

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. }  
分享到:
评论

相关推荐

    算法与数据结构-综合提升 C++版

    算法与数据结构-综合提升 C++版 资源列表: 00-0pening 01-Why-Algorithms 02-Sorting-Basic ...05-Binary-Search-Tree 06-Union-Find 07-Graph-Basics08-Minimum-Span-Trees 09-Shortest-Path 10-Ending

    算法刷题笔记leetcode/lintcode

    - Binary Search(二分查找) - Math(数学算法) - Greatest Common Divisor(最大公约数) - Prime(质数检测) - Knapsack(背包问题) - Probability(概率问题) - **Basics Misc**(其他基础知识) - ...

    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.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...

    算法-leetcode-剑指offer上的题很多

    - **二分搜索(Binary Search)**: 在有序数组中查找特定元素的搜索算法。 #### 数学基础 - **数学(Math)**: 研究数和数量、空间以及它们之间的关系。 - **质数(Prime)**: 只有1和它本身两个正因数的大于1的自然数。 ...

    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...

    算法模板.pdf

    - 二分搜索(Binary Search):在有序数组中查找目标值。 3. 数据结构 - 并查集(Disjoint Set Union):用于处理一些不相交集合的合并及查询问题。 - 最小生成树(Prim):一种用来寻找图的最小生成树的算法。 - ...

    清华邓俊辉数据结构

    - **二叉搜索树与平衡树**(Binary Search Tree and Balanced Trees):第5.Bst章节深入探讨了二叉搜索树(Binary Search Tree, BST)以及几种常见的平衡树(Balanced Tree),例如AVL树、伸展树(Splay Tree)、B树、红黑...

    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 ...

    Problem Solving with C++ (7th edition)

    - **Binary and Text Files**: Discussion of binary and text file formats and their implications on file I/O operations. **Programming Project 17.10**: This project likely involves reading from and ...

    Competitive Programmer's Handbook Antti Laaksonen

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

Global site tag (gtag.js) - Google Analytics