标签:
1 #include <stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4 #define LENGTH(x) (sizeof(x)) / (sizeof(x[0])) 5 6 int cmp(const void *a, const void *b) 7 { 8 return(strcmp((char*)a, (char*)b)); 9 } 10 11 void print(char(*arr)[10], int len) 12 { 13 for (int i = 0; i < len; i++) 14 { 15 printf("%s ", arr[i]); 16 } 17 printf("\n"); 18 } 19 20 int main() 21 { 22 char arr[][10] = { "bac", "bca", "abc", "acb", "cba", "cab" }; 23 char *key = "bca"; 24 int *p = NULL; 25 printf("before qsort:"); 26 print(arr, LENGTH(arr)); 27 qsort(arr, LENGTH(arr), sizeof(arr[0]), cmp); 28 printf("after qsort:"); 29 print(arr, LENGTH(arr)); 30 p = bsearch(key, arr, LENGTH(arr), sizeof(arr[0]), cmp); 31 if (p != NULL) 32 { 33 printf("find!"); 34 } 35 else 36 { 37 printf("no find!"); 38 } 39 return 0; 40 }
标签:
原文地址:http://www.cnblogs.com/maxin/p/5516167.html