QuickSort

Category: Arrays

Date: 02-16-2022

Return to Index


 
'Compilable Example:  (Jose Includes)
#Compile Exe
#Dim All
#Debug Error On
#Debug Display On
#Include "Win32API.inc"
Global hDlg As Dword
Global A(),B(),C(),D() As String
 
Function PBMain() As Long
   Dialog New Pixels, 0, "Test Code",300,300,200,200, %WS_OverlappedWindow To hDlg
   Control Add Button, hDlg, 100,"Push", 50,10,100,20
   ReDim D(4) : D(0)="M" : D(1) = "I" :  D(2)="J" : D(3) = "A" : D(4) = "Z"
   Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
   If Cb.Msg = %WM_Command And Cb.Ctl = 100 And Cb.CtlMsg = %BN_Clicked Then
      QuickSortSA D(), 0, UBound(D)
      ? Join$(D(),"")
   End If
End Function
 
Sub QuickSortSA (A() As String, Lower As Long, Upper As Long)
   Local pivot As String, tmpLow,tmpHi As Long
   tmpLow = Lower : tmpHi = Upper : pivot = A((Lower + Upper) / 2)
   While (tmpLow <= tmpHi)
      While (A(tmpLow) < pivot And tmpLow < Upper) : Incr tmpLow : Wend
      While (pivot < A(tmpHi) And tmpHi > Lower)   : Decr tmpHi  : Wend
      If (tmpLow <= tmpHi) Then Swap A(tmpLow), A(tmpHi) : Incr tmpLow : Decr tmpHi
   Wend
   If (Lower < tmpHi) Then QuickSortSA A(), Lower, tmpHi
   If (tmpLow < Upper) Then QuickSortSA A(), tmpLow, Upper
End Sub 
 
'gbs_01216
'Date: 05-11-2013


created by gbSnippets
http://www.garybeene.com/sw/gbsnippets.htm