简体   繁体   中英

Using the Bubble Sort Algorithm on Windows Form C# to sort random numbers?

I am suppose to be writing a C# program in visual studios windows forms application that needs to implement the following functionalities:

Create 1,000 random numbers between 1 and 5000

Use the bubble sort algorithm and sort the 1000 numbers

This is the code that I have so far:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Prog7
{
    public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void btnGenerate_Click(object sender, EventArgs e)
    {
        Random num = new Random();
        string line = Environment.NewLine;
        int nbr = num.Next(0, 5001);
        textNumbers.Text = nbr.ToString();

        for(int i = 1; i <= 1000; i++)
        {
            nbr = num.Next(0, 5001);
            textNumbers.Text = textNumbers.Text + line + nbr.ToString();
        }

    }

    private void SortBtn_Click(object sender, EventArgs e)
    {
        bool inorder = false;
        while (!inorder)
        {
            inorder = true;
            for (int i = 1; i <= 1000; i++)
            {
                if (swap(ref i, ref i + 1))
                    inorder = false;
            }
        }

        for (int i = 1; i <= 1000; i++)
        {
            nbr = num.Next(0, 5001);
            SortBox.Text = SortBox.Text + line + nbr.ToString();
        }


    }

    private bool swap(ref int top, ref int bottom)
    {
        int temp;

        if (top <= bottom)
            return false;

        temp = top;
        top = bottom;
        bottom = temp;
        return true;

    }
}
}

My original plan was to have the form have a button for generating the numbers and a button for sorting the numbers with two textboxes to list down the numbers.The code in my btnGenerate_Click works fine for generating the 1000 different numbers. But I am having a difficulty figuring out how I can input the bubble sort algorithm into this program. I looked up many examples online on what to do but many of the examples involve a array list which im not using. The program I have right now for SortBtn_Click obviously doesnt work. If anyone can give me suggestion on how to make it work or a easier way to create this program please let me know! I appreciate all the help anyone is willing to provide.

You can follow below steps to fix your issue:

  1. Instead of creating new random number in SortBtn_Click function, use text present in textNumber textbox.
  2. In SortBtn_Click function, First check textNumber is null or empty. If the string is null or empty, then throw an error.
  3. In else part, split string and convert it into integer array.

you can split string using following code.

 string[] strArr = textNumber.Text.split(Environment.NewLine);

Now convert string array into integer array.

int[] intArr = Array.ConvertAll(strArr, Int32.Parse);

Now apply bubble sort on intArr variable, store result in SortBox.Text text field

Note: As @JohnG mentioned about variables used in btnGenerate_Click (num, line, nbr) goes out of scope when execution leaves btnGenerate_click function. So you can not use those variables into other function. If you want to use those variables then declare that variables in class scope( Global declaration )

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