Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Module Module1
- Sub SortAsc(a() As Integer)
- For index As Integer = 0 To a.Length - 2
- For scan As Integer = index + 1 To a.Length - 1
- 'If a(index) > a(scan) Then
- If ConfrontaAsc(a(index), a(scan)) Then
- Dim temp As Integer = a(index)
- a(index) = a(scan)
- a(scan) = temp
- End If
- Next
- Next
- End Sub
- Sub SortDesc(a() As Integer)
- For index As Integer = 0 To a.Length - 2
- For scan As Integer = index + 1 To a.Length - 1
- ' If a(index)< a(scan) Then
- If ConfrontaDesc(a(index), a(scan)) Then
- Dim temp As Integer = a(index)
- a(index) = a(scan)
- a(scan) = temp
- End If
- Next
- Next
- End Sub
- ' è un NUOVO tipo di dato che rappresenta TUTTE le funzioni che accettano in
- ' ingresso due interi e restituiscono un Boolean
- Delegate Function CompareDelegateFunction(x As Integer, y As Integer) As Boolean
- Sub Sort(a() As Integer, func As CompareDelegateFunction)
- For index As Integer = 0 To a.Length - 2
- For scan As Integer = index + 1 To a.Length - 1
- ' Invocazione del DELEGATO "func"
- If func(a(index), a(scan)) Then
- Dim temp As Integer = a(index)
- a(index) = a(scan)
- a(scan) = temp
- End If
- Next
- Next
- End Sub
- Function ConfrontaAsc(a As Integer, b As Integer) As Boolean
- Return a > b
- End Function
- Function ConfrontaDesc(a As Integer, b As Integer) As Boolean
- Return a < b
- End Function
- Function ConfrontaRandom(a As Integer, b As Integer) As Boolean
- If a Mod 2 = 0 Then
- Return a > b
- Else
- Return b > a
- End If
- End Function
- Sub Print(a() As Integer)
- For Each n As Integer In a
- Console.WriteLine(n)
- Next
- End Sub
- Sub Main()
- Dim numbers() = {2341, 6, 1234, 47, 123, 1234, 78, 1234, 78, 1234, 48, 968234}
- Console.WriteLine("Array iniziale:")
- Print(numbers)
- Console.WriteLine("Array ordinato in maniera crescente:")
- SortAsc(numbers)
- Print(numbers)
- Console.WriteLine("Array ordinato in maniera decrescente:")
- SortDesc(numbers)
- Print(numbers)
- Console.WriteLine("Array ordinato in maniera crescente con delegato:")
- ' invocazione di SORT con PASSAGGIO della FUNZIONE DELEGATA ConfrontaAsc
- Sort(numbers, AddressOf ConfrontaAsc)
- Print(numbers)
- Console.WriteLine("Array ordinato in maniera decrescente con delegato:")
- ' invocazione di SORT con PASSAGGIO della FUNZIONE DELEGATA ConfrontaDesc
- Sort(numbers, AddressOf ConfrontaDesc)
- Print(numbers)
- Console.WriteLine("Array ordinato in maniera casuale:")
- ' invocazione di SORT con PASSAGGIO della FUNZIONE DELEGATA ConfrontaRandom
- Sort(numbers, AddressOf ConfrontaRandom)
- Print(numbers)
- Console.ReadLine()
- End Sub
- End Module
Add Comment
Please, Sign In to add comment