Visual Basic 2008 9.0 .NET Examples and Ebook
  Home

Arrays

Vorig Onderwerp

Introduction to Visual Basic

|

Procedures and Functions

Volgend Onderwerp

Sorting Arrays - Bubble Sort

Vorig Onderwerp

Sorting Arrays - Selection Sort

|

Sorting Arrays - Insertion Sort

Volgend Onderwerp
-


The bubble sort will compare all two consecutive values and switch the values if necessary. This will sink all heavy values to the bottom, and drive all light values to the surface :


           [-unsorted---------------------------------------------]
 index     0          1          2          3          4
 value     88         75         93         81         21

Values 88 and 75 are compared, 88 > 75 => switch both values :


           [-unsorted---------------------------------------------]
 index     0          1          2          3          4
 value     75         88         93         81         21

Values 88 and 93 are compared, 88 <= 93 => don't switch :


           [-unsorted---------------------------------------------]
 index     0          1          2          3          4
 value     75         88         93         81         21

Values 93 and 81 are compared, 93 > 81 => switch both values :


           [-unsorted---------------------------------------------]
 index     0          1          2          3          4
 value     75         88         81         93         21

Values 93 and 21 are compared, 93 > 21 => switch both values :


           [-unsorted--------------------------------] [-sorted---]
 index     0          1          2          3          4
 value     75         88         81         21         93

The heaviest element has now sunken to the bottom of the array. The unsorted part now starts at index 0 and ends at index 3 :

Values 75 and 88 are compared, 75 <= 88 => don't switch :


           [-unsorted--------------------------------] [-sorted---]
 index     0          1          2          3          4
 value     75         88         81         21         93

Values 88 and 81 are compared, 88 > 81 => switch both values :


           [-unsorted--------------------------------] [-sorted---]
 index     0          1          2          3          4
 value     75         81         88         21         93

Values 88 and 21 are compared, 88 > 21 => switch both values :


           [-unsorted---------------------] [-sorted--------------]
 index     0          1          2          3          4
 value     75         81         21         88         93

Values 75 and 81 are compared, 75 <= 81 => don't switch :


           [-unsorted---------------------] [-sorted--------------]
 index     0          1          2          3          4
 value     75         81         21         88         93

Values 81 and 21 are compared, 81 > 21 => switch both values :


           [-unsorted----------] [-sorted-------------------------]
 index     0          1          2          3          4
 value     75         21         81         88         93

Values 75 and 21 are compared, 75 > 21 => switch both values :


           [-sorted-----------------------------------------------]
 index     0          1          2          3          4
 value     21         75         81         88         93

Module BubbleSortExample
    Sub Main()
        Dim count As Integer = 5
        Dim upperbound As Integer = count - 1
        Dim numbers(upperbound) As Integer
        '
        Dim index As Integer
        For index = 0 To upperbound
            numbers(index) = (count ^ index) * 93 Mod 97 - (count \ (index + 1))
        Next
        '
        Console.Write("unsorted array  : ")
        For index = 0 To upperbound
            Console.Write(numbers(index) & "  ")
        Next
        Console.WriteLine()
        '
        Dim unsortedCount As Integer = count
        Do While unsortedCount > 1
            Dim endIndexUnsortedPart As Integer = unsortedCount - 2
            '
            For index = 0 To endIndexUnsortedPart
                If numbers(index) > numbers(index + 1) Then
                    Dim backup As Integer = numbers(index)
                    numbers(index) = numbers(index + 1)
                    numbers(index + 1) = backup
                End If
            Next
            '
            unsortedCount -= 1
            '
            Console.Write("temporary array : ")
            For index = 0 To upperbound
                Console.Write(numbers(index) & "  ")
            Next
            Console.WriteLine()
        Loop
        '
        Console.Write("sorted array    : ")
        For index = 0 To upperbound
            Console.Write(numbers(index) & "  ")
        Next
        '
        Console.ReadLine()
    End Sub
End Module
Download Broncode

Output :

 unsorted array  : 88  75  93  81  21
 temporary array : 75  88  81  21  93
 temporary array : 75  81  21  88  93
 temporary array : 75  21  81  88  93
 temporary array : 21  75  81  88  93
 sorted array    : 21  75  81  88  93

Updated On : 2008-10-25

Download Broncode

Published On : 2008-11-06

Sorting Arrays - Bubble Sort

Vorig Onderwerp

Sorting Arrays - Selection Sort

|

Sorting Arrays - Insertion Sort

Volgend Onderwerp

Arrays

Vorig Onderwerp

Introduction to Visual Basic

|

Procedures and Functions

Volgend Onderwerp
  Home  
Nederlands
Nederlands

Add to favorites (IE).


No printable version available.