|
|
 |
|
|
|
|
|
|
|
|
Visual Basic 2008 9.0 .NET Examples and Ebook
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| In this topic we'll discuss 3 sort methods, the selection, the bubble and the insertion sort.
To illustrate the sorting techniques well use an array contain some random numeric values, that well try to sort from low to high : |
| index 0 1 2 3 4
value 88 75 93 81 21 |
Selection Sort
| We'll use a sorted part, which contains no elements before sorting, and a unsorted part, which contains all elements before sorting.
The selection sort will keep switching the positions of the smallest value and the first value of the unsorted part. This will be repeated for an unsorted part that keeps getting smaller : |
| [-unsorted---------------------------------------------]
index 0 1 2 3 4
value 88 75 93 81 21
first smallest |
| Switch the smallest (21) and first value (88) of the unsorted part : |
| [-sorted-] [-unsorted----------------------------------]
index 0 1 2 3 4
value 21 75 93 81 88
smallest
first |
| Switch the smallest (75) and first value (75) of the unsorted part : |
| [-sorted------------] [-unsorted-----------------------]
index 0 1 2 3 4
value 21 75 93 81 88
first smallest |
| The switch was unnecessary, but harmless.
Switch the smallest (81) and first value (93) of the unsorted part : |
| [-sorted-----------------------] [-unsorted------------]
index 0 1 2 3 4
value 21 75 81 93 88
first smallest |
| Switch the smallest (88) and first value (93) of the unsorted part : |
| [-sorted----------------------------------] [-unsorted-]
index 0 1 2 3 4
value 21 75 81 88 93 |
| When the unsorted part contains only 1 element, then the complete array is sorted. |
| Module SelectionSortExample
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 startIndexUnsortedPart As Integer = count - unsortedCount
Dim indexSmallestElement As Integer = startIndexUnsortedPart
For index = startIndexUnsortedPart To upperbound
If numbers(index) < numbers(indexSmallestElement) Then
indexSmallestElement = index
End If
Next
Dim backup As Integer = numbers(indexSmallestElement)
numbers(indexSmallestElement) = numbers(startIndexUnsortedPart)
numbers(startIndexUnsortedPart) = backup
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 : 21 75 93 81 88
temporary array : 21 75 93 81 88
temporary array : 21 75 81 93 88
temporary array : 21 75 81 88 93
sorted array : 21 75 81 88 93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|