forked from TheAlgorithms/Go
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add implementation of simple sort (TheAlgorithms#395)
- Loading branch information
Showing
2 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters