C Language Search algorithm

Source: Internet
Author: User

/**

Find (Array)

1> Sequential Lookup: It is the traversal of an array, each element is compared to that element, but the problem is less efficient

2> Binary Lookup: The condition is that the array must be sequential

*/

#include <stdio.h>

#define N 2

int main ()

{

Binary find

/*

Idea: the element is compared with the intermediate element, if there is, otherwise, determine the element in which part, continue to find;

*/

Any input integer x to find an element with the same x value in an ordered array

Defining input data

int x =-1;

Arrays Array

int array[10] = {2, 10, 19, 22, 34, 36, 55, 67, 76, 88};

scanf ("%d", &x);

Define start element subscript start, tail end, middle element subscript mid

int start = 0;

int end = 9;

int mid = (start + end)/2;

while (Array[mid]! = x && (end > Start)) {

if (x < Array[mid]) {

end = Mid-1;

}else{

Start = mid + 1;

}

Mid = (start + end)/2;

}

printf ("%d\n", mid);

return 0;

}

C Language Search algorithm

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.