Task: Sorting algorithms_Quicksort




Go: package main

import "fmt"

func main() {
    list := []int{31, 41, 59, 26, 53, 58, 97, 93, 23, 84}
    fmt.Println("unsorted:", list)

    quicksort(list)
    fmt.Println("sorted!  ", list)
}

func quicksort(a []int) {
    var pex func(int, int)
    pex = func(lower, upper int) {
        for {
            switch upper - lower {
            case -1, 0: 
                return
            case 1: 
                
                if a[upper] < a[lower] {
                    
                    a[upper], a[lower] = a[lower], a[upper]
                }
                return
            
            
            }

            
            
            
            
            
            
            bx := (upper + lower) / 2
            b := a[bx]  
            lp := lower 
            up := upper 
        outer:
            for {
                
                for lp < upper && !(b < a[lp]) {
                    lp++
                }
                for {
                    if lp > up {
                        
                        break outer
                    }
                    
                    if a[up] < b {
                        break 
                    }
                    up--
                }
                
                a[lp], a[up] = a[up], a[lp]
                lp++
                up--
            }
            
            
            if bx < lp {
                
                if bx < lp-1 {
                    
                    a[bx], a[lp-1] = a[lp-1], b
                }
                up = lp - 2
            } else {
                
                if bx > lp {
                    
                    a[bx], a[lp] = a[lp], b
                }
                up = lp - 1
                lp++
            }
            
            
            if up-lower < upper-lp {
                pex(lower, up)
                lower = lp
            } else {
                pex(lp, upper)
                upper = up
            }
        }
    }
    pex(0, len(a)-1)
}





VB: Public Sub Quick(a() As Variant, last As Integer)

Dim aLess() As Variant
Dim aEq() As Variant
Dim aGreater() As Variant
Dim pivot As Variant
Dim naLess As Integer
Dim naEq As Integer
Dim naGreater As Integer

If last > 1 Then
    
    pivot = a(Int((last + 1) / 2))
    
    naLess = 0
    naEq = 0
    naGreater = 0
    For Each el In a()
      If el > pivot Then
        naGreater = naGreater + 1
        ReDim Preserve aGreater(1 To naGreater)
        aGreater(naGreater) = el
      ElseIf el < pivot Then
        naLess = naLess + 1
        ReDim Preserve aLess(1 To naLess)
        aLess(naLess) = el
      Else
        naEq = naEq + 1
        ReDim Preserve aEq(1 To naEq)
        aEq(naEq) = el
      End If
    Next
    
    Quick aLess(), naLess
    Quick aGreater(), naGreater
    
    P = 1
    For i = 1 To naLess
      a(P) = aLess(i): P = P + 1
    Next
    For i = 1 To naEq
      a(P) = aEq(i): P = P + 1
    Next
    For i = 1 To naGreater
      a(P) = aGreater(i): P = P + 1
    Next
End If
End Sub

Public Sub QuicksortTest()
Dim a(1 To 26) As Variant

 
 For i = 1 To 26: a(i) = 26 - i: Next
 Quick a(), 26
 For i = 1 To 26: Debug.Print a(i);: Next
 Debug.Print
 
 For i = 1 To 26: a(i) = Chr$(Asc("z") + 1 - i) & "-stuff": Next
 Quick a(), 26
 For i = 1 To 26: Debug.Print a(i); " ";: Next
 Debug.Print
End Sub