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