Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions internal/quick_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,51 @@ func partition[T cmp.Ordered](arr []T, lo int, hi int) int {
arr[lo], arr[j] = arr[j], arr[lo]
return j
}

// QuickSelectFunc finds the k-th smallest element in a slice using the Quickselect algorithm with a custom comparator.
// The slice is partially partitioned and may not maintain full order.
// It modifies the input slice in-place.
// T is a generic type, and the comparison logic is provided by the `compare` function.
// The `lo` and `hi` parameters define the range in the slice to consider for the selection.
func QuickSelectFunc[T any](arr []T, lo int, hi int, pivot int, compare func(a, b T) int) T {
for hi > lo {
j := partitionFunc(arr, lo, hi, compare)
if j == pivot {
return arr[pivot]
}
if j > pivot {
hi = j - 1
} else {
lo = j + 1
}
}
return arr[pivot]
}

func partitionFunc[T any](arr []T, lo int, hi int, compare func(a, b T) int) int {
i := lo
j := hi + 1
v := arr[lo]
for {
for compare(arr[i+1], v) < 0 {
i++
if i == hi {
break
}
}
i++
for compare(v, arr[j-1]) < 0 {
j--
if j == lo {
break
}
}
j--
if i >= j {
break
}
arr[i], arr[j] = arr[j], arr[i]
}
arr[lo], arr[j] = arr[j], arr[lo]
return j
}
90 changes: 90 additions & 0 deletions internal/quick_select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,93 @@ func TestQuickSelectString(t *testing.T) {

assert.Equal(t, expected, result, "want: %v\ngot: %v", expected, result)
}

type testEntry struct {
hash uint64
summary any
}

func TestQuickSelectFunc(t *testing.T) {
testCases := []struct {
name string
arr []testEntry
lo int
hi int
pivot int
expected uint64
}{
{
name: "two elements first smaller",
arr: []testEntry{{hash: 50}, {hash: 100}},
lo: 0,
hi: 1,
pivot: 1,
expected: 100,
},
{
name: "find median",
arr: []testEntry{{hash: 3}, {hash: 1}, {hash: 4}, {hash: 1}, {hash: 5}, {hash: 9}, {hash: 2}, {hash: 6}},
lo: 0,
hi: 7,
pivot: 4,
expected: 4,
},
{
name: "find minimum",
arr: []testEntry{{hash: 3}, {hash: 1}, {hash: 4}, {hash: 1}, {hash: 5}, {hash: 9}, {hash: 2}, {hash: 6}},
lo: 0,
hi: 7,
pivot: 0,
expected: 1,
},
{
name: "find maximum",
arr: []testEntry{{hash: 3}, {hash: 1}, {hash: 4}, {hash: 1}, {hash: 5}, {hash: 9}, {hash: 2}, {hash: 6}},
lo: 0,
hi: 7,
pivot: 7,
expected: 9,
},
{
name: "single element",
arr: []testEntry{{hash: 42}},
lo: 0,
hi: 0,
pivot: 0,
expected: 42,
},
{
name: "two elements descending",
arr: []testEntry{{hash: 5}, {hash: 3}},
lo: 0,
hi: 1,
pivot: 0,
expected: 3,
},
{
name: "with summary data",
arr: []testEntry{{hash: 30, summary: "a"}, {hash: 10, summary: "b"}, {hash: 20, summary: "c"}},
lo: 0,
hi: 2,
pivot: 1,
expected: 20,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
arrCopy := make([]testEntry, len(tc.arr))
copy(arrCopy, tc.arr)

result := QuickSelectFunc(arrCopy, tc.lo, tc.hi, tc.pivot, func(a, b testEntry) int {
if a.hash < b.hash {
return -1
} else if a.hash > b.hash {
return 1
}
return 0
})

assert.Equal(t, tc.expected, result.hash, "want: %v\ngot: %v", tc.expected, result.hash)
})
}
}
Loading