| 
 | 
 
 
 楼主 |
发表于 2011-4-29 08:40:48
|
显示全部楼层
 
 
 
当然实现了  我在Binary Search   (半折查找)使用的就是这种形式。 
 
#include "stdio.h" 
#include "stdlib.h" 
#include "malloc.h" 
 
int *init_array(int n) 
{ 
        return (int *)malloc(sizeof(int) * n); 
} 
 
void binary_search(int *local,int find,int length) 
{ 
        int low, high, mid; 
        low = 0; 
        high = length; 
 
        while(low <= high) 
        { 
                mid = (low + high) / 2; 
                if( local[mid] == find) 
                { 
                        printf("The element you find locaed at %dth\n", (mid + 1)); 
                        exit(0); 
                } 
                if( local[mid] < find) 
                { 
                        low = mid + 1; 
                } 
                if( local[mid] > find) 
                { 
                        high = mid -1; 
                } 
        } 
        printf("No Such Elemment Finded!!\n"); 
} 
 
int main() 
{ 
        int find,len, *local, *temp, length; 
        printf("Put The length of integer:"); 
        scanf("%d",&len); 
        length = len; 
        temp = local = init_array(len); 
        printf("The elemments of thie array:"); 
        while( len-- ) 
        { 
                scanf("%d",temp++); 
        } 
        printf("The Elemment You Want Find:"); 
        scanf("%d",&find); 
        binary_search(local, find, length); 
        return 0; 
} |   
 
 
 
 |