From 142deb83b2558dab134494cd3f45721d42bc9872 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Sat, 26 Mar 2022 18:25:42 +0800 Subject: [PATCH] fix: fix InsertionSort bug --- algorithm/sorter.go | 2 +- algorithm/sorter_test.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/algorithm/sorter.go b/algorithm/sorter.go index 075ed0f..9223337 100644 --- a/algorithm/sorter.go +++ b/algorithm/sorter.go @@ -37,7 +37,7 @@ func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator) [] preIndex-- } - slice[preIndex+1] = preItem + slice[preIndex+1] = currentItem } return slice diff --git a/algorithm/sorter_test.go b/algorithm/sorter_test.go index f6dd592..0a534dd 100644 --- a/algorithm/sorter_test.go +++ b/algorithm/sorter_test.go @@ -89,10 +89,10 @@ func TestInsertionSort(t *testing.T) { asssert := internal.NewAssert(t, "TestInsertionSort") comparator := &peopleAgeComparator{} - sortedPeopleByAge := SelectionSort(peoples, comparator) + sortedPeopleByAge := InsertionSort(peoples, comparator) t.Log(sortedPeopleByAge) - expected := "[{d 8} {b 10} {c 17} {a 20} {e 28}]" + expected := "[{e 28} {c 17} {a 20} {b 10} {d 8}]" actual := fmt.Sprintf("%v", sortedPeopleByAge) asssert.Equal(expected, actual)