跳搜索算法 [英] jump search algorithm

查看:147
本文介绍了跳搜索算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做跳跃的搜索算法,但它告诉我该元素是不是在阵,而这是 这里是code

i am doing jump search algorithm but it show me that element is not in array while it is here is code

import java.math.*; 

public class  jamp  {

    public  static int min(int a,int b) {
        return a<b?a:b;
    }

    public  static void main(String[]args) {
        int  a[]=new int[]{3,7,9,12,14,15,16,17,18};
        int l=14;
        System.out.println(jumpsearch(a,a.length,l));
    }

    public static int jumpsearch(int a[],int n, int  l ) {
        int t=0;
        int b=(int)Math.sqrt(n);
        while (a[min(b,n)-1]<t){
            t=b;
            b=b+(int)Math.sqrt(n);
            if ( t>=n)  return  -1  ;
        }
        while (a[t]<l){
            t=t+1;
            if ( t==min(b,n))    
                return   -1  ;
            if ( a[t]==l)  {
                return t;
            }
        }
        return -1;
    }
}

请帮忙

推荐答案

修改

while (a[min(b,n)-1]<t){

while (a[min(b,n)-1]<l){ // t should be l

据该值此文章应该是搜索键。当我运行这个更改的程序,我得到4。

According to this article that value should be the search key. When I run the program with this change I get 4.

这篇关于跳搜索算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆