Recursive: recursive implementation of binary search algorithms

Source: Internet
Author: User
Another typical recursive example is the binary search algorithm for sorted arrays. Now there is an array that has been sorted. you need to search for an element in this array to determine whether it is in this array. the general idea is to check each element in sequence, check whether it is the same as the element to be searched. This method is easy to think of, but its efficiency is unsatisfactory. its complexity is O (n. Now let's see if recursion is more effective here.

Another typical recursive example is the binary search algorithm for sorted arrays.

Now there is an array that has been sorted. you need to search for an element in this array to determine whether it is in this array. the general idea is to check each element in sequence, check whether it is the same as the element to be searched. This method is easy to think of, but its efficiency is unsatisfactory. its complexity is O (n. Now let's see if recursion is more effective here.

Consider the two conditions above:

  • First, can this problem be resolved into the same form but smaller scale?
  • Second: if such a decomposition exists, is there a simple situation for such decomposition?

Consideration 1: We can think like this: what should we do if we want to narrow down the problem?

We can first determine that some elements in the array are different from those to be queried, and then find them in the remaining elements, thus narrowing down the problem. How can we determine that some elements in the array are different from those to be queried? Considering that our arrays are sorted, we can compare the values and elements to be queried to determine whether the elements to be queried are in the first half or the second half of the array. In this way, we get a method to narrow down the problem.

Next, consider condition 2: What is a simple situation?

It is easy to find that if the value element is equal to the element to be queried, you can determine whether the element to be queried is in the array. this is a simple situation. Is it the only simple situation? Considering that the element is never equal to the value element, we may eventually get a small-scale array that cannot be further divided. it has only one element, then we can determine the final result by comparing the element and the element to be queried. This is also a simple situation.

Well, based on the above analysis, we found that this problem can be solved using recursion. the code of the binary method is as follows:

# Include "stdio. h "# include" stdlib. h "void selectionSort (int data [], int count); int binary_search (int * a, int n, int key); void main () {int I, key, rs; int arr [10]; int count; printf ("The array before sorting is:"); srand (int) time (0); for (I = 0; I <10; I ++) {arr [I] = rand () % 100; printf ("% d", arr [I]);} count = sizeof (arr) /sizeof (arr [0]); selectionSort (arr, count); printf ("\ n sorted array:"); for (I = 0; I <10; I ++) {printf ("% d", arr [I]);} printf ("\ n enter the number to be searched :"); scanf ("% d", & key); rs = binary_search (arr, 10, key); printf ("% d", rs );} void selectionSort (int data [], int count) {int I, j, min, temp; for (I = 0; I <count; I ++) {/* find the minimum */min = I; for (j = I + 1; j <count; j ++) if (data [j] <data [min]) min = j; temp = data [I]; data [I] = data [min]; data [min] = temp;} int binary_search (int * data, int n, int key) {int mid; if (n = 1) {return (data [0] = key);} else {mid = n/2; printf ("mid = % d \ n", data [mid]); if (data [mid-1] = key) return 1; else if (data [mid-1]> key) {printf ("key % d is smaller than data [mid-1] % d, take the first half segment \ n", key, data [mid-1]); return binary_search (& data [0], mid, key );} else {printf ("key % d is larger than data [mid-1] % d, take the second half \ n", key, data [mid-1]); return binary_search (& data [mid], n-mid, key );}}}

Program running result:

Before sorting, the array is 53 27 26 99 20 17 15 25 23 63. after sorting, the array is: 15 17 20 23 25 26 27 53 63 99. enter the number to search: 20mid = 26key 20 is smaller than data [mid-1] 25, and the first half of the mid = 20key 20 is greater than data [mid-1] 17, and the second half of the mid = 231

The complexity of this algorithm is O (logn), which is obviously better than the simple sequential lookup method previously mentioned.

Additional reading

The topic list of this article is as follows:

  1. Recursive: recursive thinking
  2. Recursion: two conditions that must be met by recursion
  3. Recursion: recursive determination of string-to-text phenomena
  4. Recursive: recursive implementation of binary search algorithms
  5. Recursion: the efficiency of recursion
  6. Recursion: recursion and loop
  7. Let's talk about recursion: Is loop and iteration the same thing?
  8. Recursive computing and iterative computing
  9. On Recursion: Learn about tail recursion from Fibonacci
  10. Recursive: Tail recursion and CPS
  11. Recursion: add more knowledge about Continuation.
  12. Recursion: Tail recursion in PHP and its optimization
  13. Recursion: Tail recursion optimization from the perspective of Assembly

This article is available at http://www.nowamagic.net/librarys/veda/detail/2317.

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.