' Visual Basic 2008 9.0 .NET Examples - Sorting Arrays - Arrays : 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 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 Module InsertionSortExample 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 - 1 Do Until unsortedCount = 0 Dim startIndexUnsortedPart As Integer = count - unsortedCount ' Dim backup As Integer = numbers(startIndexUnsortedPart) ' index = startIndexUnsortedPart - 1 Do While index >= 0 AndAlso numbers(index) > backup numbers(index + 1) = numbers(index) index -= 1 Loop ' numbers(index + 1) = 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 Module Example Sub Main() Dim count As Integer = 5 Dim upperbound As Integer = count - 1 Dim numbers(upperbound) As Integer ' Dim index As Integer Dim random As Random = New Random For index = 0 To upperbound numbers(index) = random.Next(1, 101) Next ' Console.Write("unsorted array : ") For index = 0 To upperbound Console.Write(numbers(index) & " ") Next Console.WriteLine() ' Array.Sort(numbers) ' Console.Write("sorted array : ") For index = 0 To upperbound Console.Write(numbers(index) & " ") Next ' Console.ReadLine() End Sub End Module Module ExerciseTask Sub Main() Dim count As Integer = 10 Dim currencies As String() = {"ATS", "DEM", "ESP", "FIM", "FRF", _ "IEP", "ITL", "LUF", "NLG", "PTE"} Dim conversionValues As Decimal() = {13.7603, 1.95583, 166.386, _ 5.94573, 6.55957, 0.787564, _ 1936.27, 40.3399, 2.20371, 200.482} ' Do Console.Write("Currencies : ") Dim index As Integer For index = 0 To count - 2 Console.Write(currencies(index) & " / ") Next Console.WriteLine(currencies(index)) Console.Write("Currency ? : ") Dim currency As String = Console.ReadLine() ' Dim found As Boolean ' ... binary search for currency in currencies ... ' If found Then ' ... conversion of certain amount ... Else Console.Write("Add Currency ( Y/N ) ? : ") Dim addCurrency As Char = Console.ReadLine() If addCurrency = "y"c OrElse addCurrency = "Y"c Then Console.Write("Conversion Value " & currency & _ " ( = 1 EUR ) ? : ") Dim conversionValue As Decimal = Console.ReadLine() ' ' ... insertion currency in currencies ... ' ... conversionValue in conversionValues ... End If End If Loop End Sub End Module Module ExerciseSolution Sub Main() Dim count As Integer = 10 Dim currencies As String() = {"ATS", "DEM", "ESP", "FIM", "FRF", _ "IEP", "ITL", "LUF", "NLG", "PTE"} Dim conversionValues As Decimal() = {13.7603, 1.95583, 166.386, _ 5.94573, 6.55957, 0.787564, _ 1936.27, 40.3399, 2.20371, 200.482} ' Do Console.Write("Currencies : ") Dim index As Integer For index = 0 To count - 2 Console.Write(currencies(index) & " / ") Next Console.WriteLine(currencies(index)) Console.Write("Currency ? : ") Dim currency As String = Console.ReadLine() ' Dim lowerbound As Integer = 0 Dim upperbound As Integer = count - 1 Dim middle As Integer Dim found As Boolean = False Dim done As Boolean = False Do Until found OrElse done middle = (lowerbound + upperbound) \ 2 found = (currency = currencies(middle)) If Not found Then done = (upperbound - lowerbound < 1) If Not done Then If currency > currencies(middle) Then lowerbound = middle + 1 Else upperbound = middle - 1 End If End If End If Loop ' If found Then Console.Write("Amount " & currencies(middle) & " ? : ") Dim amount As Decimal = Console.ReadLine() Console.WriteLine("Conversion : " & amount & " " & _ currencies(middle) & " = " & _ amount / conversionValues(middle) & " EUR") Else Console.Write("Add Currency ( Y/N ) ? : ") Dim addCurrency As Char = Console.ReadLine() If addCurrency = "y"c OrElse addCurrency = "Y"c Then Console.Write("Conversion Value " & currency & _ " ( = 1 EUR ) ? : ") Dim conversionValue As Decimal = Console.ReadLine() ' ReDim Preserve currencies(count) ReDim Preserve conversionValues(count) count += 1 ' index = count - 1 Do While (index - 1 >= 0) AndAlso _ (currencies(index - 1) > currency) currencies(index) = currencies(index - 1) conversionValues(index) = conversionValues(index - 1) index -= 1 Loop currencies(index) = currency conversionValues(index) = conversionValue End If End If Loop End Sub End Module ' Visit www.studyvb.com for more examples. Copyright 2003-2008 De Wolf.