Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to realize Bubble sorting in VB language

Shulou Source: shulou.com Published: 2022-06-01 02:59:59 09月30日 Update

Editor to share with you how to achieve bubble sorting in the VB language, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

The case of this issue:

Our primitive number is an array a

The data of this an array is 5 2 8 6 in turn.

Now you want to sort the data in a: from small to large

Achieve goal: 2, 5, 6, 8

Design interface

The effect after clicking the button

What is the idea of comparing the two data?

Convert ideas into code.

When comparing, it is when using the if statement.

The text in text1 is 5

The text in text2 is 2

How to compare, take out the small value?

If val (text1.text) > val (text2.text) then

Print text2.text 'here is a small value

Else

Print text1.text: here are the big values

End if

The idea of bubble sorting in this example is explained:

Bubble sort, our sort here is sorted from small to large.

The original 528 8 6 is in the array a.

The reality is:

A (0) = 5

A (1) = 2

A (2) = 8

A (3) = 6

Small idea: first find the smallest number, then find the second smallest, and gradually find it one by one.

The comparison process is as follows:

First trip: find the smallest number

Benchmark: use the first number as the benchmark. Assuming that the first number is the minimum, compare it with all the numbers that follow.

First round: 2 5 8 6

Second round: 2 5 8 6

Third round: 2 5 8 6

Result of the first trip: 2 5 8 6

Second trip: find the second smallest number

Benchmark: based on the second number.

First round: 2 5 8 6

Second round: 2 5 8 6

Result of the second trip: 2 5 8 6

Third trip: find the third smallest number

Benchmark: use the third number as the benchmark.

First round: 2 5 6 8

Let's turn the idea into code:

'start the number of trips

For iTunes 0 to 2 step 1

'start the round.

For jungle 1 to 3 muri step 1

If a (I) > a (jamboi) then'to implement interaction

Tenea (I)

A (I) = a (jyogi)

A (jamboi) = t

End if

Next j

Next i

The source code is as follows:

Option ExplicitDim a (3) As IntegerPrivate Sub Command1_Click () a (0) = 5a (1) = 2a (2) = 8a (3) = 6Dim I%, j% T% 'For I = 0 To 2 Step 1' start rounds For j = 1 To 3-I Step 1 If a (I) > a (j + I) Then'to achieve interaction t = a (I) a (I) = a (j + I) a (j + I) = t End If Next j Next i For i = 0 To 3 Step 1 Print a (I) Next iEnd Sub

These are all the contents of this article entitled "how to achieve Bubble sorting in VB language". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Number sort benchmark thought minimum data array article one round language primitive from small to big from small code content that is text time result two small Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi vpn OPPO Reno Shulou Tech Info macOS