0

ハッシュ検索メソッドの 2 番目の while ループでコードが動かなくなります。キーがデータセットにない場合、またはキーが見つかった場合、条件は最終的に false になるはずですが、条件が true のままであるように見えます。誰かがバグを見つけるのを手伝ってくれますか?

ありがとう

////// これは全体です..私はそれに取り組んでいます..まだエラーを見つけることができません。

    public void HashedSearch() 
    {
        HSAverageAccessTime       = 0;
        HSAverageCompSuc          = 0;
        HSAverageCompFailed       = 0;
        HSNumberKeysSuc           = 0;
        HSNumberKeysFailed        = 0;

        Initialize();

        int SearchKey;
        int TotalNumberOfComparisons;
        int address;
        int M;
        M = FindPrime();


        for(int i=0; i<NumberOfDataItems; i++)
        {

            address = OriginalArray[i] % M; 
            if (HashedArray[address]== -1)
                HashedArray[address]= OriginalArray[i];
            else 

            {
                address = (address+1) % M;

                while(HashedArray[address]!=-1)
                {

                    address=(address+1)%M;

                }

                HashedArray[address]=OriginalArray[i];

            }

        }

        System.out.println("after mapping" + M);
        long startTime = System.nanoTime();
        boolean found = false;

        for (int k = 0; k <NumberOfKeys; k++)
        { 

            found = false;
            SearchKey = KeysArray[k];
            TotalNumberOfComparisons = 0;


            address = KeysArray[k] % M;             
            //System.out.println("address" + address);

                //System.out.println(" inside if 1 --- address" + address);
            while ( HashedArray[address]!= SearchKey && HashedArray[address]!= -1)
            {
                if (HashedArray [address] == SearchKey)
                {
                    found = true;
                    TotalNumberOfComparisons++;
                    HSAverageCompSuc = HSAverageCompSuc + TotalNumberOfComparisons;
                    BSNumberKeysSuc ++;
                }
                else 
                {


                    System.out.println("Stuck after here");
                    HSAverageCompFailed = HSAverageCompFailed + TotalNumberOfComparisons;
                    HSNumberKeysFailed ++;

                        //address=(address+1)%M; 
                    address++;
                    }

                    System.out.println(" outside while --- found" + found);

                    //if(HashedArray[address] == SearchKey)
                        //found = true;
                    //else found = false;

                    //address=(address+1)%M;
                    //address++;
                }

            if(found)
            {
                HSAverageCompSuc = HSAverageCompSuc + TotalNumberOfComparisons;
                BSNumberKeysSuc ++;
            }
            else
            {
                HSAverageCompFailed = HSAverageCompFailed + TotalNumberOfComparisons;
                HSNumberKeysFailed ++;
            }
        }

        long estimatedTime = System.nanoTime() - startTime;

        if (NumberOfKeys != 0)
            HSAverageAccessTime = Math.round((estimatedTime/NumberOfKeys));
        else
            HSAverageAccessTime = 0;
        if(HSNumberKeysSuc != 0)
            HSAverageCompSuc        = Math.round (HSAverageCompSuc / HSNumberKeysSuc) ;
        else
            HSAverageCompSuc        = 0;
        if (HSNumberKeysFailed != 0)
            HSAverageCompFailed     = Math.round (HSAverageCompFailed / HSNumberKeysFailed) ;
        else
            HSNumberKeysFailed = 0;
        System.out.println("time after search" + estimatedTime);
        return; 
    } 
4

2 に答える 2

0
while ( HashedArray[address]!= SearchKey && HashedArray[address]!= -1)
{
        address=(address+1)%M; 

}

SearchKeyまたは-1HashedArrayにない場合、ループは終了しません。完全なコードを投稿しない限り、これ以上説明することはできません。

于 2013-03-13T22:02:59.567 に答える