[C/C++ 演算法]- 插補搜尋法
剛才找資料時發現一個C/C++的教學網站,趕快發揮(C/P)的長才將它備份來,有需要的同好,歡迎來(C/P)一下^^。
拷貝來源:
http://openhome.cc/Gossip/AlgorithmGossip/
http://openhome.cc/Gossip/AlgorithmGossip/InterpolationSearch.htm
#include <stdio.h> #include <stdlib.h> #include <time.h> #define MAX 10 #define SWAP(x,y) {int t; t = x; x = y; y = t;}
void quickSort(int[], int, int); int interpolationSearch(int[], int);
int main(void) { srand(time(NULL)); int number[MAX] = {0}; int i; for(i = 0; i < MAX; i++) { number[i] = rand() % 100; }
quickSort(number, 0, MAX-1);
printf("數列:"); for(i = 0; i < MAX; i++) printf("%d ", number[i]);
int find; printf("\n輸入尋找對象:"); scanf("%d", &find);
if((i = interpolationSearch(number, find)) >= 0) printf("找到數字於索引 %d ", i); else printf("\n找不到指定數"); printf("\n");
return 0; }
int interpolationSearch(int number[], int find) { int low = 0; int upper = MAX - 1; while(low <= upper) { int mid = (upper-low)* (find-number[low])/(number[upper]-number[low]) + low; if(mid < low || mid > upper) break; if(find < number[mid]) upper = mid - 1; else if(find > number[mid]) low = mid + 1; else return mid; } return -1; }
void quickSort(int number[], int left, int right) { if(left < right) { int s = number[(left+right)/2]; int i = left - 1; int j = right + 1;
while(1) { while(number[++i] < s) ; // 向右找 while(number[--j] > s) ; // 向左找 if(i >= j) break; SWAP(number[i], number[j]); }
quickSort(number, left, i-1); // 對左邊進行遞迴 quickSort(number, j+1, right); // 對右邊進行遞迴 } }
|
沒有留言:
張貼留言