视觉基础2015中的冒泡排序文本框

时间:2016-09-23 00:12:48

标签: visual-studio-2015

我必须制作一个程序,让用户在10个不同的文本框中输入10个不同的数字。文本框中的这些数字应从最小到最大排序(由按钮单击事件触发)。我应该创建一个数组来执行此操作。

我得到了一些提示

  • for i = 0 to 9'循环为第一,第二,第三等

  • 对于j = i + 1到9'循环为"对抗所有其他

  • 我应该像这样切换值 - > temp = x - > x = y - > y = temp !只有在x> y

  • 时才会这样做

我理解如何做到这一点,如果我为每个文本框值声明一个变量并为每个文本框值做一个if语句,我可以这样做,但我知道这不是我应该这样做的。我怎么能尽可能有效地做到这一点?

到目前为止我有什么

Public Class Form1
Dim I As Integer = 0
Dim J As Integer = 0
Dim Temp As Integer
Dim number(9) As Integer






Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

End Sub





Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    number(0) = Convert.ToInt64(TextBox1.Text)
    number(1) = Convert.ToInt64(TextBox2.Text)
    number(2) = Convert.ToInt32(TextBox3.Text)
    number(3) = Convert.ToInt64(TextBox4.Text)
    number(4) = Convert.ToInt64(TextBox5.Text)
    number(5) = Convert.ToInt64(TextBox6.Text)
    number(6) = Convert.ToInt64(TextBox7.Text)
    number(7) = Convert.ToInt64(TextBox8.Text)
    number(8) = Convert.ToInt64(TextBox9.Text)
    number(9) = Convert.ToInt64(TextBox10.Text)


    For I = 0 To 9

        If number(I) > number(I + 1) Then
            Temp = number(I)
            number(I) = number(I + 1)
            number(I + 1) = Temp

            For J = I + 1 To 9
                If I > J Then
                    Temp = number(I)
                    number(I) = number(J)
                    number(J) = Temp
                End If
                J = J + 1
            Next

        End If
        I = I + 1
    Next









End Sub

结束班

1 个答案:

答案 0 :(得分:-1)

我希望这会有所帮助:D

public sub getInputs()

    dim inputs() sa integer = {Val(textbox1.text),Val(textbox2.text),..}
    return inputs
end sub

public sub bubblesort(inputsArr() as Integer)
dim sorted as boolean = false
dim x,y as Integer
dim i as integer = 0;
while (sorted = false)
    do until i = inputs.Lenght
        'current data
        dim x = inputs(i)
        'next data
        y = inputs(i+1)
        if(y<x) then
            'Swap Data
            inputs(i) = y
            inputs(i+1)=x
            sorted = false
        end if

    i=i+1
    loop
end while
 return inputs
end sub

//然后按下按钮

dim inputs() as Integer = getInputs()
bubblesort(inputs)