Ad space (banner)
๐Ÿ”ทVB.NET Lessons
Lesson 35 / 68

Understanding how bubble sort works

This lesson covers implementing bubble sort in VB.NET, with the aim of understanding sorting from the ground up.

Bubble sort repeatedly compares neighbouring elements and swaps them when they are the wrong way round — the most basic sorting algorithm there is. The name comes from large values gradually moving towards the back like bubbles.

The sample code uses a double For statement; the inner loop compares the neighbouring elements arr(j) and arr(j + 1) and swaps them through the temporary variable tmp when they are out of order. Repeating that eventually orders the whole array.

A common early stumble is the swap itself. Writing only arr(j) = arr(j + 1) loses the original value, so you have to park one in the temporary variable tmp first.

Bubble sort is slow on large data and rarely used in practice, but it is an important algorithm for understanding what a standard library routine such as Array.Sort is doing inside.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run VB.NET directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)