forked from TheAlgorithms/Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simplesort.go
35 lines (32 loc) · 1.06 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
// 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
package sort
func SimpleSort(arr []int) []int {
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
}
// ImprovedSimpleSort 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 ImprovedSimpleSort(arr []int) []int {
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
}