Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Bubble sorting in VB language

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report