mirror of
https://github.com/duke-git/lancet.git
synced 2026-11-16 05:12:11 +08:00
fix(package): [algorithm] keep duplicate CountSort entries (#387)
This commit is contained in:
1 parent
3811d3db8e
commit
cb45ef2d36
2 files changed
+5
-1
No files matched your search
+2
-1
@@ -188,7 +188,8 @@ func CountSort[T any](slice []T, comparator constraints.Comparator) []T {
|
|||||||
count := 0
|
count := 0
|
||||||
for j := 0; j < size; j++ {
|
for j := 0; j < size; j++ {
|
||||||
//slice[i] > slice[j]
|
//slice[i] > slice[j]
|
||||||
if comparator.Compare(slice[i], slice[j]) == 1 {
|
order := comparator.Compare(slice[i], slice[j])
|
||||||
|
if order == 1 || (order == 0 && j < i) {
|
||||||
count++
|
count++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -199,6 +199,9 @@ func TestMergeSort(t *testing.T) {
|
|||||||
func TestCountSort(t *testing.T) {
|
func TestCountSort(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
assert := internal.NewAssert(t, "TestCountSort")
|
assert := internal.NewAssert(t, "TestCountSort")
|
||||||
|
assert.Equal([]int{1, 2, 2}, CountSort([]int{2, 1, 2}, &intComparator{}))
|
||||||
|
assert.Equal([]people{{"young", 10}, {"first", 20}, {"second", 20}},
|
||||||
|
CountSort([]people{{"first", 20}, {"young", 10}, {"second", 20}}, &peopleAgeComparator{}))
|
||||||
|
|
||||||
peoples := []people{
|
peoples := []people{
|
||||||
{Name: "a", Age: 20},
|
{Name: "a", Age: 20},
|
||||||
|
|||||||
Reference in new issue
Block a user