Advertisement
NelloRizzo

Sort

Sep 27th, 2019
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 1.80 KB | None | 0 0
  1. Module Module1
  2.     Sub Swap(ByRef x As Integer, ByRef y As Integer)
  3.         Dim temp As Integer = x
  4.         x = y
  5.         y = temp
  6.     End Sub
  7.  
  8.     Sub SortAsc(a() As Integer)
  9.         For i = 0 To a.Length - 2
  10.             For j = i + 1 To a.Length - 1
  11.                 If a(i) > a(j) Then
  12.                     Swap(a(i), a(j))
  13.                 End If
  14.             Next
  15.         Next
  16.     End Sub
  17.     Sub SortDesc(a() As Integer)
  18.         For i = 0 To a.Length - 2
  19.             For j = i + 1 To a.Length - 1
  20.                 If a(i) < a(j) Then
  21.                     Swap(a(i), a(j))
  22.                 End If
  23.             Next
  24.         Next
  25.     End Sub
  26.  
  27.     Sub SortAsc1(a() As Integer)
  28.         For i = 0 To a.Length - 2
  29.             For j = i + 1 To a.Length - 1
  30.                 If MustSwap(a(i), a(j)) Then
  31.                     Swap(a(i), a(j))
  32.                 End If
  33.             Next
  34.         Next
  35.     End Sub
  36.  
  37.     Function MustSwap(x As Integer, y As Integer) As Boolean
  38.         Return x > y
  39.     End Function
  40.  
  41.  
  42.     Sub Print(a() As Integer)
  43.         For Each v As Integer In a
  44.             Console.WriteLine(v)
  45.         Next
  46.     End Sub
  47.     Sub Main()
  48.         Dim a As Integer = 10
  49.         Dim b As Integer = 20
  50.         Swap(a, b)
  51.         Console.WriteLine("a = {0} b = {1}", a, b)
  52.         Dim array() As Integer = {132, 654, 859, 1234, 90678, 1234, 9}
  53.         Console.WriteLine("Array da ordinare:")
  54.         Print(array)
  55.         Console.WriteLine("Array ordinato in ordine crescente:")
  56.         SortAsc(array)
  57.         Print(array)
  58.         Console.WriteLine("Array ordinato in ordine decrescente:")
  59.         SortDesc(array)
  60.         Print(array)
  61.         Console.WriteLine("Array ordinato secondo quanto decide la funzione MustSwap:")
  62.         SortAsc1(array)
  63.         Print(array)
  64.     End Sub
  65.  
  66. End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement