简体   繁体   中英

Bubble Sort Algorithm being used for a listbox in Windows Forms C#?

I currently have a form that has four radio buttons, a list box, and a button that says "Sort". Each of the radio buttons, when clicked, produce a certain number of randomly generated integers inside of the listbox. What I want is for when I click the "Sort" button, the bubble sort algorithm that I have written will run for all the integers in the listbox. I have the bubble sort algorithm written for the button1_Click event, and I have the randomly generated integers being added to the listbox with the radioButton1_CheckedChanged event. I know the algorithm runs once I click the button, I'm just having trouble wiring that up to the array of integers that's being displayed in the listbox, so the algorithm sorts the integers in the listbox. I know I have to somehow wire the algorithm to the specific array that has been generated for the listbox, I'm just having trouble figuring out the logic behind that. Below is an excerpt of my code for the button click event, and the first radio button checked event. Right now I'm just testing my code for the first radio button, which produces 100 random integers in the listbox between 1-100. If anyone has any suggestions as to what I could try to get me going in the right direction, I would greatly appreciate it. Also, in the code below my listbox is named "Numbers", if you were wondering.

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

        for ( Out = smallMaxSize - 1; Out > 1; Out--)
        {
            for (In = 0; In < Out; In++)
            {
                if (array[In] > array[In + 1])
                {
                    swap(In, In + 1);

                }
            }
        }
    }
    #endregion

    #region Sort 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 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

Do something like this after sorting array:

Numbers.Items.Clear();

for(int i = 0; i < 101; i++) {
    Numbers.Items.Add(array[i]);
}

That should do the trick.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM