Line Search with Jagged Array?

I have the following program that creates 100 random elements through an array. These 100 random values ​​are unique, and each value is displayed only once.

Although in a linear search, he continues to search the entire array. How can I get a Jagged Array so that it only "views" the remaining places? (assuming I keep a table of 100 maximum elements, so if one random value is generated, the array contains 99 elements with linear search scanning and ...)

I guess I would have to embed a jagged array somewhere in FoundLinearInArray?

Hope this did it all. 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*/
+3
3

, , , , ? , , Hashset. hashset - O (1), (, , ) - - .

+1

, , , , . . , HashSet .

, , - Range , .

, , , . , , , .

    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);
        }
    }

, .

0
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();
    }

, JMarsch. Hashsets - .

0
source

Source: https://habr.com/ru/post/1736508/


All Articles