MCPcopy Create free account
hub / github.com/TheAlgorithms/Go / ImprovedSimple

Function ImprovedSimple

sort/simplesort.go:30–40  ·  view source on GitHub ↗

ImprovedSimple is a improve SimpleSort by skipping an unnecessary comparison of the first and last. This improved version is more similar to implementation of insertion sort

(arr []T)

Source from the content-addressed store, hash-verified

28// ImprovedSimple is a improve SimpleSort by skipping an unnecessary comparison of the first and last.
29// This improved version is more similar to implementation of insertion sort
30func ImprovedSimple[T constraints.Ordered](arr []T) []T {
31 for i := 1; i < len(arr); i++ {
32 for j := 0; j < len(arr)-1; j++ {
33 if arr[i] < arr[j] {
34 // swap arr[i] and arr[j]
35 arr[i], arr[j] = arr[j], arr[i]
36 }
37 }
38 }
39 return arr
40}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected