[Year 12 SofDev] Quick Sort Code
ATKINSON-BUCK, Damien
Damien.ATKINSON-BUCK at ivanhoe.com.au
Mon Jun 16 13:57:13 EST 2014
Hi Andrew,
This went into an empty form, I can't remember if I wrote it a couple of years ago or garnered it from elsewhere (if it was from someone else, thanks) .
Cheers
Damien
Sub SortMe(ByRef a() As Object, ByVal left As Integer, ByVal right As Integer)
Dim pivot As Integer
If right - left > 1 Then
pivot = getpivot(a, left, right)
pivot = partition(a, left, right, pivot)
SortMe(a, left, pivot)
SortMe(a, pivot + 1, right)
End If
End Sub
Sub qsort(ByRef a() As Object)
Dim i
Dim ii
For i = 0 To a.Length() - 1
ii = New System.Random().Next(0, a.Length() - 1)
If i <> ii Then
swap(a(i), a(ii))
End If
Next
SortMe(a, 0, a.Length())
End Sub
Function getpivot(ByRef a() As Object, ByVal left As Integer, ByVal right As Integer)
Return New System.Random().Next(left, right - 1)
End Function
Function partition(ByRef a() As Object, ByVal left As Integer, ByVal right As Integer, ByRef pivot As Integer)
Dim i
Dim piv
Dim store
piv = a(pivot)
swap(a(right - 1), a(pivot))
store = left
For i = left To right - 2
If a(i) <= piv Then
swap(a(store), a(i))
store = store + 1
End If
Next
swap(a(right - 1), a(store))
Return store
End Function
Sub swap(ByRef v1, ByRef v2)
Dim tmp
tmp = v1
v1 = v2
v2 = tmp
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim MyArray() As Object = {3, 7, 4, 0, 1, 12, 32, 4, 5}
Dim mystring As String = ""
Dim mystring2 As String = ""
For counter = 0 To MyArray.Length - 1
mystring = mystring & MyArray(counter) & ", "
Next
MsgBox(mystring, vbOKOnly)
qsort(MyArray)
For counter = 0 To MyArray.Length - 1
mystring2 = mystring2 & MyArray(counter) & ", "
Next
MsgBox(mystring2, vbOKOnly)
End
End Sub
Damien Atkinson-Buck
Head of Learning Area: Technology/Arts
[cid:image002.png at 01CF896A.DF1F41A0]
p: +61 3 9490 3848
e: damien.atkinson-buck at ivanhoe.com.au<mailto:damien.atkinson-buck at ivanhoe.com.au>
f: +61 3 9490 3490
w: www.ivanhoe.com.au<http://www.ivanhoe.com.au/>
From: Anthony Sullivan [mailto:asullivan at tps.vic.edu.au]
Sent: Sunday, 15 June 2014 2:12 PM
To: sofdev at edulists.com.au
Subject: [Year 12 SofDev] Quick Sort Code
I'm trying to code the Quick Sort algorithm from the textbook (page 126) into Visual Basic - but having a few problems
If anybody has a working example of this algorithm would it be possible to ask for a copy of the code?
Anthony
Anthony Sullivan
Head of Information Technology
20 Wooralla Drive, Mt. Eliza, Vic, 3930
T: 03 9788 7796 | F: 03 9787 7646 | asullivan at tps.vic.edu.au<mailto:asullivan at tps.vic.edu.au> | www.tps.vic.edu.au<http://www.tps.vic.edu.au>
[cid:image003.gif at 01CF896A.DF1F41A0]
[cid:image004.gif at 01CF896A.DF1F41A0]<http://www.facebook.com/pages/The-Peninsula-School/118935504829400?ref=hl> [cid:image005.gif at 01CF896A.DF1F41A0] <http://https:/twitter.com/TPSMtEliza> [cid:image006.gif at 01CF896A.DF1F41A0] <http://www.youtube.com/user/ThePeninsulaSchool>
Please consider the environment before printing this email, you will save 5g of paper, 250mL of water and at least 35gm of CO2
This message, including any attachments, is provided without responsibility in law for its accuracy or otherwise and without assumption of a duty of care by the school. Whilst every attempt has been made to ensure material in this e-mail message is free from computer viruses or other defects, the attached files are provided, and may only be used, on the basis that the user assumes responsibility for the use of the material transmitted. This e-mail is intended for the use of the named individual or entity and may contain confidential and privileged information. Any dissemination, distribution or copying by anyone other than the intended recipient of this e-mail is strictly prohibited. If this e-mail has been received in error then please notify The Peninsula School immediately and destroy the original message.
Privacy, Virus and Copyright Warning
The information contained in this electronic message (e-mail), and any files transmitted with it:
* is intended for the named recipients only. If you have received this in error, please advise the sender and delete it and any copies immediately;
* Any personal information in this email must be used in accordance with the Privacy Act 1988 and this always applies even if it has been sent to you in error.
* represents the views of the sender and does not necessarily represent the views or formal advice of Ivanhoe Grammar School;
* may be subject to Copyright, so no further use should be made of it without the author's permission.
The School does not represent or warrant that the email or any files attached do not contain errors or are free from computer viruses or other defects nor does it accept responsibility for any loss or damage resulting directly or indirectly from the use of the email or any attached files.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.edulists.com.au/pipermail/sofdev/attachments/20140616/0e0900d6/attachment-0001.html
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image001.png
Type: image/png
Size: 1024 bytes
Desc: image001.png
Url : http://www.edulists.com.au/pipermail/sofdev/attachments/20140616/0e0900d6/image001-0001.png
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image002.png
Type: image/png
Size: 3779 bytes
Desc: image002.png
Url : http://www.edulists.com.au/pipermail/sofdev/attachments/20140616/0e0900d6/image002-0001.png
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image003.gif
Type: image/gif
Size: 9796 bytes
Desc: image003.gif
Url : http://www.edulists.com.au/pipermail/sofdev/attachments/20140616/0e0900d6/image003-0001.gif
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image004.gif
Type: image/gif
Size: 2566 bytes
Desc: image004.gif
Url : http://www.edulists.com.au/pipermail/sofdev/attachments/20140616/0e0900d6/image004-0001.gif
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image005.gif
Type: image/gif
Size: 2578 bytes
Desc: image005.gif
Url : http://www.edulists.com.au/pipermail/sofdev/attachments/20140616/0e0900d6/image005-0001.gif
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image006.gif
Type: image/gif
Size: 2695 bytes
Desc: image006.gif
Url : http://www.edulists.com.au/pipermail/sofdev/attachments/20140616/0e0900d6/image006-0001.gif
More information about the sofdev
mailing list