C language: Write a half lookup Function

Source: Internet
Author: User

C language: Write a half lookup Function

/* First locate the intermediate element. If the element to be searched is on the left of the intermediate element, it is smaller than the intermediate element and changes right. Continue searching for intermediate elements */# include <stdio. h> # include <string. h> int zheban (int x, char arr [], int left, int right) {while (left <= right) {int mid = left-(left-right) /2; if (x> arr [mid]) {left = mid + 1;} else if (x <arr [mid]) {right = mid-1 ;} else {return 1 ;}} return 0 ;}int main () {int mid = 0; char arr [] = {1, 3, 4, 6, 7, 8, 56, 67, 78 }; int ret = 0; int size = 0; int num = 0; size = strlen (arr); ret = zheban (5, arr, 0, size-1 ); if (ret = 0) {printf ("not exsit") ;}else {printf ("% d", arr [mid]) ;}return 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.