-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
simplesort.go
40 lines (36 loc) · 1.23 KB
/
simplesort.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// simplesort.go
// description: Implementation of a simple sorting algorithm
// details:
// A simple sorting algorithm that look counter intuitive at first glance and very similar to Exchange Sort
// An improved version is included with slight changes to make the sort slightly more efficient
// reference: https://arxiv.org/abs/2110.01111v1
// see sort_test.go for a test implementation, test function TestSimple and TestImprovedSimple
// worst-case time complexity: O(n^2)
// average-case time complexity: O(n^2)
// space complexity: O(1)
package sort
import "github.com/TheAlgorithms/Go/constraints"
func Simple[T constraints.Ordered](arr []T) []T {
for i := 0; i < len(arr); i++ {
for j := 0; j < len(arr); j++ {
if arr[i] < arr[j] {
// swap arr[i] and arr[j]
arr[i], arr[j] = arr[j], arr[i]
}
}
}
return arr
}
// 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
func ImprovedSimple[T constraints.Ordered](arr []T) []T {
for i := 1; i < len(arr); i++ {
for j := 0; j < len(arr)-1; j++ {
if arr[i] < arr[j] {
// swap arr[i] and arr[j]
arr[i], arr[j] = arr[j], arr[i]
}
}
}
return arr
}