各种查找方法代码:顺序查找,折半查找,斐波那契查找,插补查找,二叉查找树查找
顺序查找
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 5 #define MAX 100 6 7 struct element 8 { 9 int key; 10 }; 11 12 typedef struct element record; 13 record data[MAX]; 14 15 int seq_search(int key) 16 { 17 int pos; 18 19 pos = 0; 20 while(pos < MAX) 21 { 22 if(key == data[pos].key) 23 return pos; 24 pos++; 25 } 26 return -1; 27 } 28 29 int main() 30 { 31 int checked[300]; 32 int i,temp; 33 long temptime; 34 srand(time(&temptime)%60); 35 for(i = 0;i < 300;i++) 36 checked[i] = 0; 37 i = 0; 38 while(i != MAX) 39 { 40 temp = rand()%300; 41 if(checked[temp] == 0) 42 { 43 data[i].key = temp; 44 checked[temp] = 1; 45 i++; 46 } 47 } 48 while(1) 49 { 50 printf("\n请输入查找值(0-299) ==>"); 51 scanf("%d",&temp); 52 if(temp != -1) 53 { 54 i = seq_search(temp); 55 if(i != -1) 56 printf("找到查找值:%d[%d]\n",temp,i); 57 else 58 printf("没有找到查找值:%d\n",temp); 59 } 60 else 61 exit(1); 62 } 63 64 return 0; 65 }
折半查找
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 #define MAX 21 5 struct element 6 { 7 int key; 8 }; 9 typedef struct element record; 10 record data[MAX] = { 11 2,5,7,9,17,21,25,33,46,89,100,121,127, 12 139,237,279,302,356,455,467,500 13 }; 14 15 int binarysearch(int key) 16 { 17 int low; 18 int high; 19 int mid; 20 21 low = 0; 22 high = MAX - 1; 23 while(low <= high) 24 { 25 mid = (low + high) / 2; 26 if(key < data[mid].key) 27 high = mid - 1; 28 else 29 if(key > data[mid].key) 30 low = mid + 1; 31 else 32 return mid; 33 } 34 return -1; 35 } 36 37 int main() 38 { 39 int found; 40 int value; 41 42 while(1) 43 { 44 printf("\n请输入查找值(0-500) ==> "); 45 scanf("%d",&value); 46 if(value != -1) 47 { 48 found = binarysearch(value); 49 if(found != -1) 50 printf("找到查找值:%d[%d]\n",value,found); 51 else 52 printf("没有找到查找值:%d\n",value); 53 } 54 else 55 exit(1); 56 } 57 58 return 0; 59 }
改写为递归的折半查找
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 #define MAX 21 5 struct element 6 { 7 int key; 8 }; 9 typedef struct element record; 10 record data[MAX] = { 11 2,5,7,9,17,21,25,33,46,89,100,121,127, 12 139,237,279,302,356,455,467,500 13 }; 14 15 int binaryfind(int key,int left,int right) 16 { 17 int mid; 18 19 if(left == right) 20 if(data[left].key == key) 21 return left; 22 else 23 return -1; 24 else 25 { 26 mid = (left + right) / 2; 27 if(mid == left) 28 mid++; 29 if(key < data[mid].key) 30 return binaryfind(key,left,mid - 1); 31 else 32 return binaryfind(key,mid,right); 33 } 34 } 35 36 int binarysearch(int key) 37 { 38 return binaryfind(key,0,MAX - 1); 39 } 40 41 int main() 42 { 43 int found; 44 int value; 45 46 while(1) 47 { 48 printf("\n请输入查找值(0-500) ==> "); 49 scanf("%d",&value); 50 if(value != -1) 51 { 52 found = binarysearch(value); 53 if(found != -1) 54 printf("找到查找值:%d[%d]\n",value,found); 55 else 56 printf("没有找到查找值:%d\n",value); 57 } 58 else 59 exit(1); 60 } 61 62 return 0; 63 }
斐波那契查找
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 #define MAX 21 5 struct element 6 { 7 int key; 8 }; 9 typedef struct element record; 10 record data[MAX] = { 11 2,5,7,9,17,21,25,33,46,89,100,121,127, 12 139,237,279,302,356,455,467,500 13 }; 14 15 int fib(int n) 16 { 17 if(n <= 1) 18 return n; 19 else 20 return fib(n-2) + fib(n-1); 21 } 22 23 int fibindex(int n) 24 { 25 int temp; 26 temp = 1; 27 while(fib(temp) <= n + 1) 28 temp++; 29 return temp - 1; 30 } 31 32 int fibsearch(int n,int key) 33 { 34 int index; 35 int mid; 36 int fn1; 37 int fn2; 38 int temp; 39 40 index = fibindex(n + 1); 41 mid = fib(index - 1); 42 fn1 = fib(index - 2); 43 fn2 = fib(index - 3); 44 while(mid != 0) 45 { 46 if(key < data[mid - 1].key) 47 if(fn2 <= 0) 48 mid = 0; 49 else 50 { 51 mid = mid - fn2; 52 temp = fn1; 53 fn1 = fn2; 54 fn2 = temp - fn2; 55 } 56 else 57 if(key > data[mid - 1].key) 58 if(fn1 <= 1) 59 mid = 0; 60 else 61 { 62 mid = mid + fn2; 63 fn1 = fn1 - fn2; 64 fn2 = fn2 - fn1; 65 } 66 else 67 return mid - 1; 68 } 69 return -1; 70 } 71 72 73 int main() 74 { 75 int found; 76 int value; 77 78 while(1) 79 { 80 printf("\n请输入查找值(0-500) ==> "); 81 scanf("%d",&value); 82 if(value != -1) 83 { 84 found = fibsearch(MAX,value); 85 if(found != -1) 86 printf("找到查找值:%d[%d]\n",value,found); 87 else 88 printf("没有找到查找值:%d\n",value); 89 } 90 else 91 exit(1); 92 } 93 94 return 0; 95 }
插补查找
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 #define MAX 20 5 struct element 6 { 7 int key; 8 }; 9 typedef struct element record; 10 record data[MAX] = { 11 2,5,7,9,17,21,25,33,46,48,51, 12 52,63,69,77,78,80,89,94,99 13 }; 14 15 int interfind(int key,int left,int right) 16 { 17 int next_guess; 18 int offset; 19 int range; 20 int index_range; 21 int temp; 22 23 if(data[left].key == key) 24 return left; 25 else 26 if(left == right || data[left].key == data[right].key) 27 return -1; 28 else 29 { 30 index_range = right - left; 31 range = data[right].key - data[left].key; 32 offset = key - data[left].key; 33 temp = (offset * index_range) / range; 34 next_guess = left + temp; 35 if(next_guess == left) 36 next_guess++; 37 if(key < data[next_guess].key) 38 return interfind(key,left,next_guess - 1); 39 else 40 return interfind(key,next_guess,right); 41 } 42 } 43 44 int intersearch(int n,int key) 45 { 46 if(key < data[0].key || key > data[n - 1].key) 47 return -1; 48 else 49 return interfind(key,0,n - 1); 50 } 51 52 int main() 53 { 54 int found; 55 int value; 56 57 while(1) 58 { 59 printf("\n请输入查找值(0-500) ==> "); 60 scanf("%d",&value); 61 if(value != -1) 62 { 63 found = intersearch(MAX,value); 64 if(found != -1) 65 printf("找到查找值:%d[%d]\n",value,found); 66 else 67 printf("没有找到查找值:%d\n",value); 68 } 69 else 70 exit(1); 71 } 72 73 return 0; 74 }
二叉查找树查找法
1 #include <stdlib.h> 2 #include <stdio.h> 3 struct tree 4 { 5 int data; 6 struct tree* left; 7 struct tree* right; 8 }; 9 10 typedef struct tree treenode; 11 typedef treenode* btree; 12 13 btree insertnode(btree root,int value) 14 { 15 btree newnode; 16 btree current; 17 btree back; 18 19 newnode = (btree)malloc(sizeof(treenode)); 20 newnode->data = value; 21 newnode->left = NULL; 22 newnode->right = NULL; 23 if(root == NULL) 24 { 25 return newnode; 26 } 27 else 28 { 29 current = root; 30 while(current != NULL) 31 { 32 back = current; 33 if(current->data > value) 34 current = current->left; 35 else 36 current = current->right; 37 } 38 if(back->data > value) 39 back->left = newnode; 40 else 41 back->right = newnode; 42 } 43 return root; 44 } 45 46 btree createbtree(int *data,int len) 47 { 48 btree root = NULL; 49 int i; 50 for(i = 0;i < len;i++) 51 root = insertnode(root,data[i]); 52 return root; 53 } 54 55 btree btreesearch(btree ptr,int value) 56 { 57 while(ptr != NULL) 58 { 59 if(ptr->data == value) 60 return ptr; 61 else 62 if(ptr->data > value) 63 ptr = ptr->left; 64 else 65 ptr = ptr->right; 66 } 67 return NULL; 68 } 69 70 int main() 71 { 72 btree root = NULL; 73 btree ptr = NULL; 74 int value; 75 int data[9] = {55,61,41,87,24,35,79,11,99}; 76 root = createbtree(data,9); 77 while(1) 78 { 79 printf("请输入查找数据(0-99) ==> "); 80 scanf("%d",&value); 81 if(value != -1) 82 { 83 ptr = btreesearch(root,value); 84 if(ptr != NULL) 85 printf("二叉查找树查找:结点数据是 [%d]\n",ptr->data); 86 else 87 printf("二叉查找树查找:没有找到\n"); 88 } 89 else 90 exit(1); 91 } 92 return 0; 93 }