¶þ·Ö²éÕÒÓÖ³ÆÕÛ°ë²éÕÒ£¬ÓŵãÊDZȽϴÎÊýÉÙ£¬²éÕÒËٶȿ죬ƽ¾ùÐÔÄܺÃ;ÆäȱµãÊÇÒªÇó´ý²é±íΪÓÐÐò±í£¬ÇÒ²åÈëɾ³ýÀ§ÄÑ¡£Òò´Ë£¬ÕÛ°ë²éÕÒ·½·¨ÊÊÓÃÓÚ²»¾³£±ä¶¯¶ø²éÕÒƵ·±µÄÓÐÐòÁÐ±í¡£Ê×ÏÈ£¬¼ÙÉè±íÖÐÔªËØÊÇ°´ÉýÐòÅÅÁУ¬½«±íÖмäλÖüǼµÄ¹Ø¼ü×ÖÓë²éÕҹؼü×ֱȽϣ¬Èç¹ûÁ½ÕßÏàµÈ£¬Ôò²éÕҳɹ¦;·ñÔòÀûÓÃÖмäλÖüǼ½«±í·Ö³ÉÇ°¡¢ºóÁ½¸ö×Ó±í£¬Èç¹ûÖмäλÖüǼµÄ¹Ø¼ü×Ö´óÓÚ²éÕҹؼü×Ö£¬Ôò½øÒ»²½²éÕÒÇ°Ò»×Ó±í£¬·ñÔò½øÒ»²½²éÕÒºóÒ»×Ó±í¡£Öظ´ÒÔÉϹý³Ì£¬Ö±µ½ÕÒµ½Âú×ãÌõ¼þµÄ¼Ç¼£¬Ê¹²éÕҳɹ¦£¬»òÖ±µ½×Ó±í²»´æÔÚΪֹ£¬´Ëʱ²éÕÒ²»³É¹¦¡£
search.h
#ifndef _SEARCH_H_ #define _SEARCH_H_
void Search(int *a,int num,int n);
#endif |
search.c
#include <stdio.h> #include "search.h"
/************************************** º¯ÊýµÄÃû£ºsearch º¯ÊýµÄ¹¦ÄÜ£º¶þ·Ö²éÕÒ º¯ÊýµÄ²ÎÊý£º¿Õ ×÷Õߣº ÈÕÆÚ£º
******************************************/
void Search(int *a,int num,int n) { int left = 0; int right = n-1; int mid = (left+right)/2;
while(a[mid] != num&&left<right) { if(a[mid] >num) { right = mid -1; } else if(a[mid] < num) { left = mid +1; } mid = (left+right)/2; }
if(a[mid] == num) { printf("²éÕҵĽá¹ûÖУºÕâ¸öֵΪ£º%d\n",num); } else { printf("²éÕÒûÓÐÕâ¸öÖµ\n"); } } |
main.c
#include <stdio.h> #include "search.h"
int main () { int a[] = {30,44,66,22,48,89,100,20,1,3,6,88}; int n = sizeof(a)/sizeof(int); int i,j; for(i = 0;i<n-1;i++) { for(j = 0;j<n-i-1;j++) { if(a[j]>a[j+1]) { int tmp = a[j]; a[j] = a[j+1]; a[j+1] = tmp; } } } for(i = 0;i<n;i++) { printf(" %d",a[i]); } printf("\n"); int num; while(1) { printf("ÇëÊäÈëÄãÒª²éÕÒµÄÊý¾Ý:\n"); scanf("%d",&num); Search(a,num,n);
} return 0; } |