使用锯齿状数组进行线性搜索?

发布于 2024-08-25 04:00:29 字数 948 浏览 3 评论 0原文

我有以下程序,它通过数组创建 100 个随机元素。 这 100 个随机值是唯一的,每个值仅显示一次。

尽管使用线性搜索,它仍然会查找整个数组。 我怎样才能将锯齿状数组放入其中,以便它只“扫描”剩下的剩余位置? (假设我将表保持在 100 个最大元素,因此如果生成一个随机值,则数组通过线性搜索扫描保存 99 个元素,等等)

我假设我必须在 FoundLinearInArray 中的某处实现锯齿状数组?

希望这有道理。 问候。

 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*/

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*/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花辞树 2024-09-01 04:00:29

所以看起来您似乎想要填充数组,并且想要保证其中的每个项目都是唯一的?如果是这样,请将生成的每个数字放入哈希集中。哈希集上的查找是 O(1)(或者可能是对数)——您可以将一百万个项目放入其中,并且仍然具有极高的查找性能。

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.

紧拥背影 2024-09-01 04:00:29

我不确定这是否是您正在寻找的,但这里有一段代码,它用一定范围内的一组唯一随机数填充数组。正如 JMarsch 所建议的,它使用 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);
        }
    }

我希望这有帮助。

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.

逆夏时光 2024-09-01 04:00:29
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 的建议类似。哈希集是必经之路。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文