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