Preface :
Array search algorithms start with a target value and employ some indexing strategy to visit the elements looking for a match. If the target is found, the corresponding index of the matching element becomes the return value. Otherwise, the return value indicates that the target is not found. A search frequently wants to cover the entire array. A more general algorithm allows for searches in a sublist of the array. A sublist is a sequence of elements whose range of indices begin at index first and continue up to, but not including, index last. We denote a sublist by its index range [first, last).
Sequence Search :
The simplest search algorithm for an array is the sequential search. The algorithm begins with a target value and indices that define the sublist range. It scans the elements in the sublist item by item, looking for the first occurrence of a match with an item called target. If successful, the algorithm returns the index of the match. The search is not successful if the scan reaches the index last before matching the target. In this case, the algorithm returns -1.
The seqSearch() Method :
We develop the method seqSearch() for an integer array as a static method in the class Arrays. As we progresses, we will develop additional searching algorithms and add them to this class. For arguments, the method has an array arr, the two indices first and last that specify the index range, and thetarget value. The return value is an index of type int. To illustrate the action of seqSearch(), consider the integer array :
int[] arr = {6, 4, 2, 9, 5, 3, 10, 7};
The below figure illustrates a search of the entire array for target 3 and a search for the sublist [2,5) for target :
The implementation of seqSearch() uses a for-loop to scan the array elements in the range [first, last). Iterations continue so long as the index is in range and no match is found. The scan terminates on a match and the index becomes the return value. Otherwise, -1 is the return value.
Binary Search :
The sequential search is a general search algorithm that applies to any array. It methodically scans the elements one after another until it finds a match or reaches the end of the range. You can employ a more efficient search strategy for an ordered array. You use the strategy when looking up a phone number in a telephone book. Suppose you want to find the number for "Swanson". A phone book maintains names in alphabetical order, so you look for "Swanson" somewhere near the back of the book. This approach is clearly superior to a sequential search, which would laboriously scan the "A"s, then the "B"s, and subsequent letters in the alphabet. Our problem is to turn the rather haphazard phone number lookup strategy into an algorithm that will work for any ordered array. The solution is the binary search algorithm. Given a target value, the algorithm begins the search by selecting the midpoint in the list. If the value at the midpoint matches the target, the search is done. Otherwise, because the list is ordered, the search should continue in the first half of the list (lower sublist) or in the second half sublist of the list (upper sublist). If the target is less than the current midpoint value, look in the lower sublist by selecting its midpoint; otherwise, look in the upper sublist by selecting its midpoint. You can continue the process by looking at midpoints for ever smaller and smaller sublists. Eventually, the process either finds the target value or reduce the size of the sublist to 0, which is the criterion that the target is not in the list.
To get a more formal understanding of the binary search algorithm, we need to specify the meaning of midpoint and lower and upper sublist in terms of array indices. Assume arr is an array with n items and that the search looks for the item called target. The indices for the full list are in the index range range [0,n). Start the search process by computing the middle index for the range [first, last), and then assign the value at the index to the variable midValue :
mid = (first + last) /2; //middle index for [first, last)
midValue = arr[mid]; //save value in midValue
Compare midValue with target. Three possible outcomes can occur, with trigger three separate actions :
Below figure gives a snapshots of the binary search algorithm as it looks for a target in the nine-element integer array, arr.
The binSearch Method :
The static method binSearch() in the Arrays class implements the binary search algorithm for an integer array. The method has four arguments that identify the array, the indices first and last that specify the index range, and the target. The method returns the array index that identifies the first occurrence of a match of -1 if the target is not found. The implementation uses an iterative process on progressively smaller sublists [first, last). Iteration continues so long as the sublist is not empty (first < last) and no match occurs. After determines the middle index of the range and the corresponding array value, multiple selection statements compare the value with the target and treat the three possible outcomes :
Supplement :
* [ 資料結構 小學堂 ] 搜尋 : 二元搜尋法
This is a blog to track what I had learned and share knowledge with all who can take advantage of them
標籤
- [ 英文學習 ]
- [ 計算機概論 ]
- [ 深入雲計算 ]
- [ 雜七雜八 ]
- [ Algorithm in Java ]
- [ Data Structures with Java ]
- [ IR Class ]
- [ Java 文章收集 ]
- [ Java 代碼範本 ]
- [ Java 套件 ]
- [ JVM 應用 ]
- [ LFD Note ]
- [ MangoDB ]
- [ Math CC ]
- [ MongoDB ]
- [ MySQL 小學堂 ]
- [ Python 考題 ]
- [ Python 常見問題 ]
- [ Python 範例代碼 ]
- [心得扎記]
- [網路教學]
- [C 常見考題]
- [C 範例代碼]
- [C/C++ 範例代碼]
- [Intro Alg]
- [Java 代碼範本]
- [Java 套件]
- [Linux 小技巧]
- [Linux 小學堂]
- [Linux 命令]
- [ML In Action]
- [ML]
- [MLP]
- [Postgres]
- [Python 學習筆記]
- [Quick Python]
- [Software Engineering]
- [The python tutorial]
- 工具收集
- 設計模式
- 資料結構
- ActiveMQ In Action
- AI
- Algorithm
- Android
- Ansible
- AWS
- Big Data 研究
- C/C++
- C++
- CCDH
- CI/CD
- Coursera
- Database
- DB
- Design Pattern
- Device Driver Programming
- Docker
- Docker 工具
- Docker Practice
- Eclipse
- English Writing
- ExtJS 3.x
- FP
- Fraud Prevention
- FreeBSD
- GCC
- Git
- Git Pro
- GNU
- Golang
- Gradle
- Groovy
- Hadoop
- Hadoop. Hadoop Ecosystem
- Java
- Java Framework
- Java UI
- JavaIDE
- JavaScript
- Jenkins
- JFreeChart
- Kaggle
- Kali/Metasploit
- Keras
- KVM
- Learn Spark
- LeetCode
- Linux
- Lucene
- Math
- ML
- ML Udemy
- Mockito
- MPI
- Nachos
- Network
- NLP
- node js
- OO
- OpenCL
- OpenMP
- OSC
- OSGi
- Pandas
- Perl
- PostgreSQL
- Py DS
- Python
- Python 自製工具
- Python Std Library
- Python tools
- QEMU
- R
- Real Python
- RIA
- RTC
- Ruby
- Ruby Packages
- Scala
- ScalaIA
- SQLAlchemy
- TensorFlow
- Tools
- UML
- Unix
- Verilog
- Vmware
- Windows 技巧
- wxPython
訂閱:
張貼留言 (Atom)
[Git 常見問題] error: The following untracked working tree files would be overwritten by merge
Source From Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 # git clean -d -fx 方案2: 今天在服务器上 gi...
-
前言 : 為什麼程序管理這麼重要呢?這是因為: * 首先,本章一開始就談到的,我們在操作系統時的各項工作其實都是經過某個 PID 來達成的 (包括你的 bash 環境), 因此,能不能進行某項工作,就與該程序的權限有關了。 * 再來,如果您的 Linux 系統是個...
-
屬性 : 系統相關 - 檔案與目錄 語法 : du [參數] [檔案] 參數 | 功能 -a | 顯示目錄中個別檔案的大小 -b | 以bytes為單位顯示 -c | 顯示個別檔案大小與總和 -D | 顯示符號鏈結的來源檔大小 -h | Hum...
-
來源自 這裡 說明 : split 是 Perl 中非常有用的函式之一,它可以將一個字串分割並將之置於陣列中。若無特別的指定,該函式亦使用 RE 與 $_ 變數 語法 : * split /PATTERN/,EXPR,LIMIT * split /...
沒有留言:
張貼留言