Sequential search and Binary Search
I. Sequential Search Ideas
1. Scan from one end of the table, scan the linear table sequentially, and compare the node keywords scanned in sequence with the given value K. if the keyword of the node currently scanned is equal to the given value K, the search is successful. If the keyword is not found after the scan, the search fails;
2. Sequential search is applicable to both sequential storage structures and chain storage structures of linear tables;
3. ASL = (n + 1)/2 indicates the average search length.
4. Advantages: the algorithm is simple and there is no requirement for the storage structure. Disadvantages: space waste. Low efficiency when the length is very large
5. Algorithm Description:
/*** Sequential search * @ param int a [] indicates the database to be searched * @ param int length indicates the length of array a * @ param int key indicates the target object to be searched *@ return int */int orderSerch (int a [], int length, int key) {int count = 0; if (key> a [length-1]) {cout <"your input element does not exist! "<
A [I]) {count ++; I ++;} else if (key = a [I]) {cout <"found successfully and found successfully! "<
Ii. Basic Idea of Binary Search
Binary Search, also known as semi-query, has the advantage of a small number of times, fast query speed, and good average performance. Its disadvantage is that the table to be queried is an ordered table and it is difficult to insert or delete data. Therefore, the half-fold lookup method is suitable for searching frequently ordered lists without frequent changes. First, assume that the elements in the table are arranged in ascending order and the keywords recorded in the middle of the table are compared with the search keywords. If the two are the same, the search is successful; otherwise, the table is divided into the first and last sub-tables by using the intermediate position record. If the keyword recorded in the middle position is greater than the search keyword, the former sub-table is further searched. Otherwise, the latter sub-table is further searched. Repeat the preceding process until you find a record that meets the conditions to make the search successful, or until the child table does not exist, the search fails.
The half-lookup method is also called the binary lookup method. It fully utilizes the order relationship between elements and adopts the grouping policy. In the worst case, it can use O (log n) to complete the search task. The basic idea is to divide n elements into two halves with roughly the same number. Take a [n/2] for comparison with x to be searched, if x = a [n/2], locate x and terminate the algorithm. If xa [n/2] is returned, we only need to search for x in the right half of array. These are all copied from Baidu Library, which is easy to understand. This algorithm is too simple to be discussed here.
Paste the Code:
/*** Binary Search ** @ param int a [] indicates the database to be searched * @ param int length indicates the length of array a * @ param int key indicates the target object to be searched *@ return int */int BinarySerch (int a [], int length, int key) {int count = 0; if (key> a [length-1]) {cout <"your input element does not exist! "<
Key) {high = mid-1 ;}}if (high = mid & a [mid]! = Key) {cout <"Search times:" <
Paste all the code: Here I put the sequential search and binary search together to facilitate observation and learning, and use the Quick Sort for sorting, A beginner like me is a complete and learnable code.
# Include
# Include
# Include
Using namespace std; int recode [2] = {0}; // The Code void QKSort (int a [], int low, int high) implemented by recursive sorting) {if (low> = high) {return;} int I = low; int j = high; int key = a [low]; while (I! = J) {for (; j! = I; j --) {if (a [j] <= key) {a [I] = a [j]; break ;}} for (; I! = J; I ++) {if (a [I]> key) {a [j] = a [I]; break ;}} a [I] = key; QKSort (a, low, I-1); QKSort (a, j + 1, high );} /*** sequential search * @ param int a [] indicates the database to be searched * @ param int length indicates the length of array a * @ param int key indicates the target object to be searched *@ return int */int orderSerch (int a [], int length, int key) {int count = 0; if (key> a [length-1]) {cout <"your input element does not exist! "<
A [I]) {count ++; I ++;} else if (key = a [I]) {cout <"found successfully and found successfully! "<
A [length-1]) {cout <"the element you entered does not exist! "<
Key) {high = mid-1 ;}}if (high = mid & a [mid]! = Key) {cout <"Search times:" <
> Input; if (input <0 | input> 3) {cout <"Please reselect:" <
4) {system ("CLS"); operation (a, length); count1 = 0;} switch (input) {case 1: cout <"performs Binary Search ---" <
> Key; BinarySerch (a, length, key); cout <"Please choose whether to continue operation: Y/N" <
> C; if (c = 'y' | c = 'y') {operation (a, length );} else if (c = 'n' | c = 'n') {return;} else {cout <"incorrect input! "<
> Key; orderSerch (a, length, key); cout <"Please choose whether to continue operation: Y/N" <
> C; if (c = 'y' | c = 'y') {operation (a, length );} else if (c = 'n' | c = 'n') {return;} else {cout <"incorrect input! "<
> C; if (c = 'y' | c = 'y') {operation (a, length );} else if (c = 'n' | c = 'n') {return;} else {cout <"incorrect input! "<