1

I am practicing the bubble sort algorithm in Windows Forms. I have gotten my program to the point where the algorithm works, and I can generate numbers in a listbox from 1-100 in non-numerical order, and then run the bubble sort algorithm to sort the numbers. The problem I'm having is that I don't want any duplicate numbers to be generating in the listbox. Basically, I have a radio button that produces 100 integers in the listbox when clicked. Then I have a "sort" button that will sort through those numbers in the listbox. I am currently using arrays to do this, as an array is easiest to use when writing the bubble sort algorithm and for creating random numbers in the listbox (that's why I'm not creating a new list and attempting to shuffle it that way). If anyone has any suggestions as to what I could do to prevent duplicates, I'd greatly appreciate it. Here is an excerpt of my code below.

int[] array;
int smallMaxSize = 101;
#region Sort Button Click
    private void button1_Click(object sender, EventArgs e)//Bubblesort Code
    {
        bubbleSortAlgorithm(smallMaxSize);
    }
    #endregion

    #region Swap Numbers
    private void swap(int one, int two)//Swaps larger number for smaller number
    {
        int temp = array[one];
        array[one] = array[two];
        array[two] = temp;
    }
    #endregion

    #region Bubble Sort Algorithm
    private void bubbleSortAlgorithm(int Size)
    {
        int Out;
        int In;

        for (Out = Size - 1; Out > 1; Out--)
        {
            for (In = 0; In < Out; In++)
            {
                if (array[In] > array[In + 1])
                {
                    swap(In, In + 1);
                    Numbers.Items.Clear();
                    for (int i = 0; i < Size; i++)
                    {
                        Numbers.Items.Add(array[i]);
                    }
                }
            }
        }
    }
    #endregion

    #region Small Max: 100
    private void radioButton1_CheckedChanged(object sender, EventArgs e)//Max 100 button
    {
        Numbers.Items.Clear();
        array = new int[smallMaxSize];

        Random numGenerator = new Random();

        numGenerator.Next(smallMaxSize);

        for (int i = 0; i < 101; i++)//Generates 100 random numbers from 1-100
        {
            array[i] = numGenerator.Next(smallMaxSize);
            Numbers.Items.Add(array[i]);
        }  
    }
    #endregion

1 Answers1

2

When you need to generate 100 random numbers from 1 to 100, inclusive, with no duplicates, actually generating them with numGenerator.Next(smallMaxSize) and checking for duplicates is very inefficient. A better approach is to fill the array with the 100 numbers that you want, in sorted order, and then apply a shuffle algorithm to the list.

This Q&A explains how to shuffle a list using Fisher-Yates algorithm.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • The problem with that, is that I also have three other radio buttons that produce 1000, 10000, and 100000 integers respectively in the list box as well. So I need a time effective way to produce those integers in random order. I will check out the Fisher Yates algorithm you suggested though. –  May 28 '16 at 15:06
  • 1
    @HylianSith Why does that matter? You can do the same thing there. Generating the numbers would be as easy as `Enumerable.Range(1, n).OrderBy(n => n).Shuffle()` where you can set n to 100, 100, 10000, etc – Rob May 28 '16 at 15:13
  • @Rob that's true. This is the first time making a random number generator without duplicates with using arrays, so that's why I'm struggling. I'll try writing the Fisher-Yates algorithm tonight and see if I have any results. –  May 28 '16 at 15:17