This commit is contained in:
Kovid Goyal
2022-09-21 10:51:43 +05:30
parent d0e133885c
commit e7f38929d9
2 changed files with 7 additions and 5 deletions

View File

@@ -9,10 +9,11 @@ import (
var _ = fmt.Print
func Reverse[T any](s []T) {
func Reverse[T any](s []T) []T {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
return s
}
func Reversed[T any](s []T) []T {
@@ -23,10 +24,12 @@ func Reversed[T any](s []T) []T {
return ans
}
func Sort[T any](s []T, less func(a, b T) bool) {
func Sort[T any](s []T, less func(a, b T) bool) []T {
sort.Slice(s, func(i, j int) bool { return less(s[i], s[j]) })
return s
}
func StableSort[T any](s []T, less func(a, b T) bool) {
func StableSort[T any](s []T, less func(a, b T) bool) []T {
sort.SliceStable(s, func(i, j int) bool { return less(s[i], s[j]) })
return s
}