C Speech Order search algorithm and code

Source: Internet
Author: User

Search is a complex search algorithm, the completion of the method is to start from the beginning of the sequence of elements, each sequence of elements and the elements to be found to stop comparing, if the sequence has elements and the elements to be found equal, then search for victory, If the survey finds that the first element of the sequence does not have an element equal to the value of the element being searched for, then it indicates that the search is lost. Next a piece of code to understand the order of the detailed use of the search.

 #include  <stdio.h>  #include  < stdlib.h>  #include  <memory.h> int ordersearch (Int a[], int n,  int des) { int i; for (i=0; i<n; i++)  if (Des==a[i])  return  1; return 0; } int main () { int i, val; int a[8] =  {32,12,56,78,76,45,43,98}; int ret; for (i=0; i<8; i++)  printf ("%d\t",  a[i]);  printf ("\ n Please output the element you are looking for:"),  while (1) { scanf ("%d",  &val);  fflush (stdin);  ret = ordersearch (A, 8, val);  if (1 == ret)  printf  ("Find victory! ; else printf  ("Find out!")  printf ("\ n Please output the element you are looking for:");  } return 0; } 

Operational consequences:

32 12 56 78 76 45 43 98 Enter the element you want to find: 78 Find Victory! Please enter the element you are looking for: 5 Find out!

Analysis of the following operational consequences, the initial output to find the element is 78, the number in the sequence to be found is present, so the print input "find victory!" ”。 The next output of the value 5 does not exist in the sequence to be searched, so the print input "Find out!" ”。


This article is from the "11999725" blog, please be sure to keep this source http://12009725.blog.51cto.com/11999725/1843308

C Speech Order search algorithm and code

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.