Linear Search with Jagged Array?

Posted by Nerathas on Stack Overflow See other posts from Stack Overflow or by Nerathas
Published on 2010-03-11T15:53:43Z Indexed on 2010/03/27 4:43 UTC
Read the original article Hit count: 221

Filed under:

Hello,

I have the following program that creates 100 random elements trough a array. Those 100 random value's are unique, and every value only gets displayed once.

Although with the linear search it keeps looking up the entire array. How would i be able to get a Jagged Array into this, so it only "scans" the remaining places left? (assuming i keep the table at 100 max elements, so if one random value is generated the array holds 99 elements with linear search scans and on...)

I assume i would have to implent the jagged array somewhere in the FoundLinearInArray?

Hopefully this made any sence. Regards.

 private int ValidNumber(int[] T, int X, int Range)
    {
        Random RndInt = new Random();
        do
        {
            X = RndInt.Next(1, Range + 1);
        } while (FoundLinearInArray(T, X));

        return X; 

    }/*ValidNumber*/

    private bool FoundLinearInArray(int[] A, int X)
    {
        byte I = 0;
        while ((I < A.Length) && (A[I] != X))
        {
            I++;
        }
        return (I < A.Length);
    }/*FoundInArray*/


    public void FillArray(int[] T, int Range)
    {
        for (byte I = 0; I < T.Length; I++)
        {
            T[I] = ValidNumber(T, I, Range);
        }

    }/*FillArray*/

© Stack Overflow or respective owner

Related posts about c#