Search series-Brief sequential search and Binary Search

Source: Internet
Author: User
Tags recode

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! "<Endl ;}for (INT I = 0; I <length;) {If (Key> A [I]) {count ++; I ++ ;} else if (Key = A [I]) {cout <"found successfully and found successfully! "<Endl; cout <" Search times: "<count <Endl; recode [0] = count; return I ;} else {cout <"not found, search failed! "<Endl; cout <" Search times: "<count <Endl; Return-1 ;}}}
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 x <A [n/2] is returned, we only need to search for X in the left half of array A (Here we assume that the array elements are in ascending order ). If X> A [n/2], we only need to continue searching 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! "<Endl; Return-1;} int low = 0, high = length-1; // low and high indicate the low and high levels of the sequence table, point to the first position and the last position of the sequence table, respectively, int mid = 0; // The while (low <= high) {count ++ used to record the median; mid = (low + high)/2; if (a [Mid] = Key) {cout <"Search times:" <count <Endl; recode [1] = count; cout <"search successful! "<Endl; return 1;} if (a [Mid] <key) {LOW = Mid + 1;} if (a [Mid]> key) {high = mid-1 ;}}if (high = Mid & A [Mid]! = Key) {cout <"Search times:" <count <Endl; cout <"search failed! "<Endl; Return-1 ;}}
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 <iostream> # include <stdlib. h> # include <windows. h> using namespace STD; int recode [2] = {0}; // The Code void qksort (int A [], int low, int high) implemented in 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! "<Endl ;}for (INT I = 0; I <length;) {If (Key> A [I]) {count ++; I ++ ;} else if (Key = A [I]) {cout <"found successfully and found successfully! "<Endl; cout <" Search times: "<count <Endl; recode [0] = count; return I ;} else {cout <"not found, search failed! "<Endl; cout <" Search times: "<count <Endl; Return-1 ;}}} /*** 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! "<Endl; Return-1;} int low = 0, high = length-1; // low and high indicate the low and high levels of the sequence table, point to the first position and the last position of the sequence table, respectively, int mid = 0; // The while (low <= high) {count ++ used to record the median; mid = (low + high)/2; if (a [Mid] = Key) {cout <"Search times:" <count <Endl; recode [1] = count; cout <"search successful! "<Endl; return 1;} if (a [Mid] <key) {LOW = Mid + 1;} if (a [Mid]> key) {high = mid-1 ;}}if (high = Mid & A [Mid]! = Key) {cout <"Search times:" <count <Endl; cout <"search failed! "<Endl; Return-1 ;}// menu void menu () {cout <" | ---------------------------------------------------- | "<Endl; cout <"| ---------- 1. Binary Search --------------- |" <Endl; cout <"| ---------- 2. Sequential search ------------- |" <Endl; cout <"| ---------- 3. Comparison of search times --------------- |" <Endl; cout <"| average |" <Endl;} int count1 = 0; void operation (int A [], int length) {menu (); count1 ++; in T key = 0; cout <"select the required service:" <Endl; int input = 0; CIN> input; if (input <0 | input> 3) {cout <"Please reselect:" <Endl; Operation (A, length);} Char C = 0; cout <"-----" <count1 <Endl; If (count1> 4) {system ("CLS"); operation (A, length); count1 = 0 ;} switch (input) {Case 1: cout <"performs Binary Search ---" <Endl; cout <"Enter the element to be searched :"; cin> key; binaryserch (A, length, key); cout <"Please choose whether to continue operation: Y/N" <Endl; CIN> C; if (C = 'y' | C = 'y') {operation (A, length);} else if (C = 'n' | C = 'n') {return;} else {cout <"incorrect input! "<Endl;} Case 2: cout <" sequential Search --- "<Endl; cout <" Enter the element to be searched :"; cin> key; orderserch (A, length, key); cout <"Please choose whether to continue operation: Y/N" <Endl; CIN> C; if (C = 'y' | C = 'y') {operation (A, length );} else if (C = 'n' | C = 'n') {return;} else {cout <"incorrect input! "<Endl;} Case 3: cout <" Search times: "<Endl; cout <" binary search times: "<recode [1] <Endl; cout <" sequential search times: "<recode [0] <Endl; cout <"select whether to continue operation: Y/N" <Endl; CIN> C; If (C = 'y' | C = 'y ') {operation (A, length);} else if (C = 'n' | C = 'n') {return ;} else {cout <"incorrect input! "<Endl; Operation (A, length) ;}} int main () {int A [33] =, ,}; qksort (A,); cout <"database element: "<Endl; For (INT I = 0; I <33; I ++) {cout <A [I] <" ;}cout <Endl; operation (A, 33); System ("pause"); Return 0 ;}
The Code has been verified!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.