Binary Search of basic algorithms and binary search of Algorithms

Source: Internet
Author: User

Binary Search of basic algorithms and binary search of Algorithms

Binary Search algorithm C

Binary Search also belongs to the search range of the ordered table. Binary Search is also called a half-fold search. The time complexity of Binary Search (ordered) is O (LogN ).

The basic idea of binary search is to take the intermediate record as the comparison object in an ordered table. If the given value is the same as the keyword of the intermediate record, the search is successful; if the given value is less than the keyword of the intermediate record, you can continue searching in the left half of the intermediate record. If the given value is greater than the keyword of the intermediate record, you can continue searching in the right half of the intermediate record. Repeat the above process until it is found.

From the definition of binary search, we can see that binary search has two prerequisites:

1. The list to be searched must be ordered.

2. The ordered storage structure of a linear table must be used to store data.

 

Binary lookup is efficient when it comes to a large number of Ordered arrays. Here we take the most regular array as an example. The Code is as follows:

1 # include <stdio. h> 2 // Binary Search 3 int binary_search (const int c [], int l, int h, int key); 4 void Display (int x ); 5 int a [] = {15, 8, 300,}; 6 int z = sizeof (a)/sizeof (int ); 7 int B [sizeof (a)/sizeof (int)]; 8 int t; 9 int I; 10 int main (int argc, const char * argv []) {11 12 for (I = 0; I <(sizeof (a)/sizeof (int); I ++) {13 B [I] = a [I]; 14} 15 // printf ("% ld \ n", sizeof (a)/sizeof (int); 16 1 7 for (I = 0; I <(sizeof (a)/sizeof (int); I ++) 18 {19 for (int j = I; j <(sizeof (a)/sizeof (int); j ++) 20 {21 if (B [I]> B [j]) {22 t = B [I]; 23 B [I] = B [j]; 24 B [j] = t; 25} 26} 27} 28 printf ("\ n"); 29 int m = 0; 30 int n = z; 31 int w; 32 int x; 33 printf ("Enter the integer to be searched:"); 34 scanf ("% d", & x); 35 w = x; 36 // getchar (); 37 int g; 38g = binary_search (B, m, n, w); 39 Display (g); 40 return 0; 41} 42 43 int binary_search (const int c [], int l, int h, I Nt key) {44 while (l <= h) 45 {46 int f = (l + h)/2; 47 if (c [f] = key) 48 return c [f]; 49 // at the left half side 50 else if (c [f]> key) 51 h = f-1; 52 // 53 else54 l = f + 1; 55} 56 // 57 return-1 not found; 58} 59 60 // print the result 61 void Display (int x) {62 if (x! =-1) {63 for (I = 0; I <z; I ++) {64 if (x = a [I]) {65 printf ("location: % d \ n ", I); 66} 67} 68} 69 70 else {71 printf (" sorry, this number \ n "is not found); 72} 73}

Result 1:

Enter the integer to be searched: 200 location: 10 Program ended with exit code: 0

Result 2:

Enter the integer to be searched: 500 sorry, this number cannot be found Program ended with exit code: 0

 

Related Article

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.