Download code

From LiteratePrograms

Jump to: navigation, search

Back to Quicksort_(Visual_Basic_.NET)

Download for Windows: single file, zip

Download for UNIX: single file, zip, tar.gz, tar.bz2

quicksort.vb

  1 ' Copyright (c) 2010 the authors listed at the following URL, and/or
  2 ' the authors of referenced articles or incorporated external code:
  3 ' http://en.literateprograms.org/Quicksort_(Visual_Basic_.NET)?action=history&offset=20070726234304
  4 ' 
  5 ' Permission is hereby granted, free of charge, to any person obtaining
  6 ' a copy of this software and associated documentation files (the
  7 ' "Software"), to deal in the Software without restriction, including
  8 ' without limitation the rights to use, copy, modify, merge, publish,
  9 ' distribute, sublicense, and/or sell copies of the Software, and to
 10 ' permit persons to whom the Software is furnished to do so, subject to
 11 ' the following conditions:
 12 ' 
 13 ' The above copyright notice and this permission notice shall be
 14 ' included in all copies or substantial portions of the Software.
 15 ' 
 16 ' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 17 ' EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 18 ' MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 19 ' IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 20 ' CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 21 ' TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 22 ' SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 23 ' 
 24 ' Retrieved from: http://en.literateprograms.org/Quicksort_(Visual_Basic_.NET)?oldid=10766
 25 
 26 
 27 Module Quicksort
 28     Sub swap(ByRef v1, ByRef v2)
 29         Dim tmp
 30         tmp = v1
 31         v1 = v2
 32         v2 = tmp
 33     End Sub
 34 
 35     Function partition(ByRef a() As Object, ByVal left As Integer, _
 36     ByVal right As Integer, ByRef pivot As Integer)
 37         Dim i
 38         Dim piv
 39         Dim store
 40 
 41         piv = a(pivot)
 42 
 43         swap(a(right - 1), a(pivot))
 44 
 45         store = left
 46         For i = left To right - 2
 47             If a(i) <= piv Then
 48                 swap(a(store), a(i))
 49                 store = store + 1
 50             End If
 51         Next
 52         swap(a(right - 1), a(store))
 53 
 54         Return store
 55     End Function
 56 
 57     Function getpivot(ByRef a() As Object, ByVal left As Integer, ByVal right As Integer)
 58         return New System.Random().Next(left, right - 1)
 59     End Function
 60 
 61     Sub quicksort(ByRef a() As Object, ByVal left As Integer, ByVal right As Integer)
 62         Dim pivot As Integer
 63         If right - left > 1 Then
 64             pivot = getpivot(a, left, right)
 65             pivot = partition(a, left, right, pivot)
 66             quicksort(a, left, pivot)
 67             quicksort(a, pivot + 1, right)
 68         End If
 69     End Sub
 70 
 71     Sub qsort(ByRef a() As Object)
 72         Dim i
 73         Dim ii
 74         For i=0 to a.Length()-1
 75             ii=New System.Random().Next(0, a.Length()-1)
 76             If i<>ii Then
 77                 swap(a(i), a(ii))
 78             End If
 79         Next
 80 
 81         quicksort(a, 0, a.Length())
 82     End Sub
 83 
 84     Sub print(ByRef a() As Object)
 85         Dim i
 86         For i = 0 To a.Length() - 1
 87             System.Console.Write("  " & a(i))
 88         Next
 89         System.Console.WriteLine()
 90     End Sub
 91 
 92     Sub main()
 93         Dim towns() As Object = {"Paris", "London", "Stockholm", "Berlin", "Oslo", "Rome", _
 94         "Madrid", "Tallinn", "Amsterdam", "Dublin"}
 95 
 96         System.Console.Write("towns before qsort: ")
 97         print(towns)
 98 
 99         qsort(towns)
100 
101         System.Console.Write("towns after qsort: ")
102         print(towns)
103 
104         Dim numbers() As Object = {5, 7, 5, 2, 8, 4, 6, 2, 3, 5, 1, 8, 5, 7, 3}
105 
106         System.Console.Write("numbers before qsort: ")
107         print(numbers)
108 
109         qsort(numbers)
110 
111         System.Console.Write("numbers after qsort: ")
112         print(numbers)
113     End Sub
114 End Module
115 
116 


Views
Personal tools