P
P
Pavel Buglaev2018-08-27 18:39:14
Visual Basic
Pavel Buglaev, 2018-08-27 18:39:14

How to sort by bubble method in visual basic?

I need to write a bubble sort code with manual number entry and a flowchart. I found a code with random numbers, but I need to enter manually...

Dim m(10)
Dim u As Integer
Private Sub Command1_Click()
Form1.ForeColor = vbRed
Form1.FontSize = 12
List1.Clear: List2.Clear
For i = 1 To 10
m(i) = Int(Rnd * 10) + 1
List1.AddItem m(i)
Next i
End Sub

Private Sub Command2_Click()
For i = 1 To 10
For j = 1 To 9
If m(j) > m(j + 1) Then
u = m(j)
m(j) = m(j + 1)
m(j + 1) = u
End If
Next j
Next i
For i = 1 To 10
List2.AddItem m(i)
Next i
End Sub

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene Chefranov, 2018-08-28
@Chefranov

Module Module1
 
    Sub Main()
 
        Console.WriteLine("Введите восемь чисел")
        'Массив для хранения восьми чисел
        Dim nums(7) As Integer
        For i As Integer = 0 To nums.Length - 1
            Console.Write("{0}-е число: ", i + 1)
            nums(i) = Int32.Parse(Console.ReadLine())
        Next
 
        'Алгоритм сортировки
        Dim temp As Integer = nums(0)
        For i As Integer = 0 To nums.Length - 1
            For j As Integer = i + 1 To nums.Length - 1
                If nums(i) > nums(j) Then
                    temp = nums(i)
                    nums(i) = nums(j)
                    nums(j) = temp
                End If
            Next
        Next
 
        'Выводим элементы массива
        For Each i As Integer In nums
            Console.Write("{0} ", i)
        Next
 
        Console.ReadLine()
 
    End Sub
 
End Module

https://metanit.com/visualbasic/tutorial/2.7.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question