一般的静态查找方法
import java.lang.reflect.Array;
import java.util.Arrays;
public class StaticSearch {
static int SequenceSearch1(int[] arr, int k)
{
for (int i = 0; i < arr.length; i++)
{
if (arr[i] == k)
return i+1;
}
return -1;
}
static int SequenceSearch2(int[] arr, int k)
{
int i;
int temp[] = new int[arr.length+1];
System.arraycopy(arr, 0, temp, 0, 10);
temp[10]=k;
for (i = 0; temp[i] != k; i++);
return i+1 ;
}
static int BinarySearch1(int[] arr, int k)
{
int low=0;
int high=arr.length-1;
while(low<=high)
{
int mid = (low+high)/2;
if(k==arr[mid])
return mid+1;
else if(k<arr[mid])
high=mid-1;
else
low=mid+1;
}
return -1;
}
static int BinarySearch2(int[] arr, int k,int low, int high)
{
while(low<=high) {
int mid = (low+high)/2;
if(k==arr[mid])
return mid+1;
else if(k<arr[mid])
return BinarySearch2(arr,k,low,mid-1);
else
return BinarySearch2(arr,k,mid+1,high);
}
return -1;
}
static int InsertionSearch(int[] arr, int k)
{
int low=0;
int high=arr.length-1;
while(low<=high)
{
int mid = low+(k-arr[low])/(arr[high]-arr[low])*(high-low);
if(k==arr[mid])
return mid+1;
else if(k<arr[mid])
high=mid-1;
else
low=mid+1;
}
return -1;
}
final static int max_size=100;
static void Fibonacci(int[] F) {
F[0]=1;
F[1]=2;
for(int i=2;i<max_size;i++) {
F[i]=F[i-1]+F[i-2];
}
}
static int FibonacciSearch(int[] arr, int k)
{
int low=0;
int high=arr.length-1;
int F[]=new int[max_size];
Fibonacci(F);
int p=0;
while(arr.length>F[p]-1)
++p;
int temp[]=new int[F[p]-1];
for(int i=0;i<arr.length;i++)
temp[i]=arr[i];
for(int i=arr.length;i<F[p]-1;i++)
temp[i]=arr[arr.length-1];
while(low<=high)
{
int mid = low+F[p-1]-1;
if(k<temp[mid]) {
high=mid-1;
p-=1;
}
else if(k>temp[mid]) {
low=mid+1;
p-=2;
}
else {
if(mid<arr.length)
return mid+1;
else
return arr.length;
}
}
return -1;
}
public static void main(String[] arg)
{
int array[] = { 3, 6, 4, 8, 1, 0, 34, 7, 22, -2 };
System.out.println("排序前:"+Arrays.toString(array));
System.out.println(SequenceSearch1(array, 34) != -1 ? "普通顺序查找结果:第" + SequenceSearch1(array, 34) + "个数" : "普通顺序查找结果:不存在");
System.out.println(SequenceSearch2(array, 34) <=array.length ? "哨兵顺序查找结果:第" + SequenceSearch2(array, 34) + "个数" : "哨兵顺序查找结果:不存在");
Arrays.sort(array);
System.out.println("排序后:"+Arrays.toString(array));
System.out.println(BinarySearch1(array,34) != -1 ? "折半非递归查找结果:转化为有序数组后在第" + BinarySearch1(array,34) + "个数" : "折半非递归查找结果:不存在");
System.out.println(BinarySearch2(array,34,0,array.length-1) != -1 ? "折半递归查找结果:转化为有序数组后在第" + BinarySearch2(array,34,0,array.length-1) + "个数" : "折半递归查找结果:不存在");
System.out.println(InsertionSearch(array, 34) != -1 ? "插入查找结果:转化为有序数组后在第" + InsertionSearch(array, 34) + "个数" : "插入查找结果:不存在");
System.out.println(FibonacciSearch(array, 34) != -1 ? "斐波那契查找结果:转化为有序数组后在第" + FibonacciSearch(array, 34) + "个数" : "斐波那契查找结果:不存在");
}
}
分块查找
1、Java实现
import java.util.Collections;
import java.util.Comparator;
import java.util.ArrayList;
import java.util.Arrays;
class Index {
int key;
int start;
public Index(int key, int start) {
this.key = key;
this.start = start;
}
}
public class BlockSearch {
static ArrayList<Index> indexs = new ArrayList<>();
static int search(int key, int a[]) {
int i=0, startValue;
while (i < 3 && key > indexs.get(i).key) {
i++;
}
if (i >= 3) {
return -1;
}
startValue = indexs.get(i).start;
while (startValue <= startValue + 5 && a[startValue] != key) {
startValue++;
}
if (startValue > startValue + 5) {
return -1;
}
return startValue;
}
public static void test() {
int i, j = -1, k, key, start;
int a[] = { 33, 42, 44, 38, 24, 48, 22, 12, 13, 8, 9, 20, 60, 58, 74, 49, 86, 53 };
for (i = 0; i < 3; i++) {
start = j + 1;
key = a[start];
j += 6;
for (k = start; k <= j; k++) {
if (a[k] >= key) {
key = a[k];
}
}
indexs.add(new Index(key,start));
}
System.out.println("排序前:");
for (Index index : indexs) {
System.out.println("关键值key:" + index.key + " 起始地址start:" + index.start);
}
Collections.sort(indexs, new Comparator<Index>() {
public int compare(Index o1, Index o2) {
return o1.key - o2.key;
}
});
System.out.println("排序后:");
for (Index index : indexs) {
System.out.println("关键值key:" + index.key + " 起始地址start:" + index.start);
}
for (i = 0; i < 18; i++) {
k = search(a[i], a);
if (k >= 0) {
System.out.println("查找成功!您要找的数" + a[i] + "在数组中的位置是:" + (k + 1));
} else {
System.out.println("查找失败!您要找的数不在数组中。");
}
}
}
}
2、C实现
#include <stdio.h>
#include <stdlib.h>
struct index
{
int key;
int start;
} newIndex[3];
int search(int key, int a[]);
int cmp(const void *a,const void* b)
{
return (*(struct index*)a).key>(*(struct index*)b).key?1:-1;
}
int main()
{
int i, j=-1, k, key;
int a[] = {33,42,44,38,24,48, 22,12,13,8,9,20, 60,58,74,49,86,53};
for (i=0; i<3; i++)
{
newIndex[i].start = j+1;
j += 6;
for (int k=newIndex[i].start; k<=j; k++)
{
if (newIndex[i].key<a[k])
{
newIndex[i].key = a[k];
}
}
}
qsort(newIndex,3, sizeof(newIndex[0]), cmp);
printf("请输入您想要查找的数:\n");
scanf("%d", &key);
k = search(key, a);
if (k>0)
{
printf("查找成功!您要找的数在数组中的位置是:%d\n",k+1);
}
else
{
printf("查找失败!您要找的数不在数组中。\n");
}
return 0;
}
int search(int key, int a[])
{
int i, startValue;
i = 0;
while (i<3 && key>newIndex[i].key)
{
i++;
}
if (i>=3)
{
return -1;
}
startValue = newIndex[i].start;
while (startValue <= startValue+5 && a[startValue]!=key)
{
startValue++;
}
if (startValue>startValue+5)
{
return -1;
}
return startValue;
}