all repos

scratch @ 4eff1ebb0d1633167671e5370c2bd4e803d72c83

⭐ me doing recreational ~~drugs~~ programming
2 files changed, 45 insertions(+), 0 deletions(-)
algos: qs
Author: Oleksandr Smirnov olexsmir@gmail.com
Committed at: 2026-04-02 13:51:09 +0300
Change ID: qsunnkvvyvqnlsxmntmlmqxznquqqxvw
Parent: efe3f18
A algos/quicksort.go
···
                
                1
                +package algos

              
                
                2
                +

              
                
                3
                +func partition(inp []int, lo, hi int) int {

              
                
                4
                +	pivot := inp[hi] // might result in O(n^2)

              
                
                5
                +

              
                
                6
                +	idx := lo - 1

              
                
                7
                +	for i := lo; i < hi; i++ {

              
                
                8
                +		if inp[i] <= pivot {

              
                
                9
                +			idx++

              
                
                10
                +			inp[i], inp[idx] = inp[idx], inp[i]

              
                
                11
                +		}

              
                
                12
                +	}

              
                
                13
                +

              
                
                14
                +	idx++

              
                
                15
                +	inp[hi], inp[idx] = inp[idx], pivot

              
                
                16
                +	return idx

              
                
                17
                +}

              
                
                18
                +

              
                
                19
                +func qs(inp []int, lo, hi int) {

              
                
                20
                +	if lo >= hi {

              
                
                21
                +		return

              
                
                22
                +	}

              
                
                23
                +

              
                
                24
                +	pivot := partition(inp, lo, hi)

              
                
                25
                +	qs(inp, lo, pivot-1)

              
                
                26
                +	qs(inp, pivot+1, hi)

              
                
                27
                +}

              
                
                28
                +

              
                
                29
                +func QuickSort(input []int) { qs(input, 0, len(input) - 1) }

              
A algos/quicksort_test.go
···
                
                1
                +package algos

              
                
                2
                +

              
                
                3
                +import (

              
                
                4
                +	"reflect"

              
                
                5
                +	"testing"

              
                
                6
                +)

              
                
                7
                +

              
                
                8
                +func TestQuicksort(t *testing.T) {

              
                
                9
                +	inp := []int{9, 3, 7, 4, 69, 420, 42}

              
                
                10
                +	out := []int{3, 4, 7, 9, 42, 69, 420}

              
                
                11
                +

              
                
                12
                +	QuickSort(inp)

              
                
                13
                +	if !reflect.DeepEqual((inp), out) {

              
                
                14
                +		t.Fatalf("i fucked up")

              
                
                15
                +	}

              
                
                16
                +}