开发者

Linear Search with Jagged Array?

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 display开发者_Go百科ed 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*/


So it looks as though you want to fill your array, and you want to guarantee that each item in it is unique? If so, put each number that you generate into a Hashset. Lookups on the hashset are O(1), (or maybe logarithmic) -- you can put a million items into it, and still have extremely high performance lookups.


I'm not certain if this is what you were looking for, but here is a snippet of code that fills an array with a set of unique random numbers within a range. As JMarsch suggested, it uses a HashSet to keep track of the numbers that are used.

It also makes a quick check to be certain that the problem is solvable - if Range is smaller than the size of the array, then you won't have enough unique numbers to fill the array.

Lastly, it creates the random number generator once rather than multiple times, which gives a better distribution of numbers. Since the numbers must be unique, you'd never notice in this case, but it is a good habit to practice, I think.

    private int GenerateUniqueRandomNumber(Random chaos, HashSet<int> used, int range)
    {
        while (true)
        {
            int candidate = chaos.Next(range);
            if (!used.Contains(candidate))
            {
                used.Add(candidate);
                return candidate;
            }
        }
    }



    public void FillArray(int[] array, int range)
    {
        if (range < array.Length)
        {
            throw new ArgumentException("Range is too small");
        }

        Random chaos = new Random();
        HashSet<int> used = new HashSet<int>();

        for (int i = 0; i < array.Length; i++)
        {
            array[i] = GenerateUniqueRandomNumber(chaos, used, range);
        }
    }

I hope this helps.


static int[] FillArray(int low, int high, int count)
    {
        Random rand = new Random();
        HashSet<int> Data = new HashSet<int>();
        while (Data.Count() < count)
            Data.Add(rand.Next(low, high));
        return Data.ToArray();
    }

This is similar to what JMarsch was suggesting. Hashsets are the way to go.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜