mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-23 13:52:26 +08:00
test: add examples for sort function
This commit is contained in:
@@ -6,7 +6,8 @@ package algorithm
|
|||||||
|
|
||||||
import "github.com/duke-git/lancet/v2/lancetconstraints"
|
import "github.com/duke-git/lancet/v2/lancetconstraints"
|
||||||
|
|
||||||
// BubbleSort use bubble to sort slice.
|
// BubbleSort applys the bubble sort algorithm to sort the collection, will change the original collection data.
|
||||||
|
// Play: https://go.dev/play/p/GNdv7Jg2Taj
|
||||||
func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator) {
|
func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator) {
|
||||||
for i := 0; i < len(slice); i++ {
|
for i := 0; i < len(slice); i++ {
|
||||||
for j := 0; j < len(slice)-1-i; j++ {
|
for j := 0; j < len(slice)-1-i; j++ {
|
||||||
@@ -18,7 +19,8 @@ func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// InsertionSort use insertion to sort slice.
|
// InsertionSort applys the insertion sort algorithm to sort the collection, will change the original collection data.
|
||||||
|
// Play: https://go.dev/play/p/G5LJiWgJJW6
|
||||||
func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator) {
|
func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator) {
|
||||||
for i := 0; i < len(slice); i++ {
|
for i := 0; i < len(slice); i++ {
|
||||||
for j := i; j > 0; j-- {
|
for j := i; j > 0; j-- {
|
||||||
@@ -32,7 +34,8 @@ func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// SelectionSort use selection to sort slice.
|
// SelectionSort applys the selection sort algorithm to sort the collection, will change the original collection data.
|
||||||
|
// Play:
|
||||||
func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator) {
|
func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator) {
|
||||||
for i := 0; i < len(slice); i++ {
|
for i := 0; i < len(slice); i++ {
|
||||||
min := i
|
min := i
|
||||||
@@ -45,7 +48,8 @@ func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ShellSort shell sort slice.
|
// ShellSort applys the shell sort algorithm to sort the collection, will change the original collection data.
|
||||||
|
// Play: https://go.dev/play/p/3ibkszpJEu3
|
||||||
func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator) {
|
func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator) {
|
||||||
size := len(slice)
|
size := len(slice)
|
||||||
|
|
||||||
@@ -64,7 +68,8 @@ func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// QuickSort quick sorting for slice, lowIndex is 0 and highIndex is len(slice)-1
|
// QuickSort quick sorting for slice, lowIndex is 0 and highIndex is len(slice)-1.
|
||||||
|
// Play: https://go.dev/play/p/7Y7c1Elk3ax
|
||||||
func QuickSort[T any](slice []T, comparator lancetconstraints.Comparator) {
|
func QuickSort[T any](slice []T, comparator lancetconstraints.Comparator) {
|
||||||
quickSort(slice, 0, len(slice)-1, comparator)
|
quickSort(slice, 0, len(slice)-1, comparator)
|
||||||
}
|
}
|
||||||
@@ -93,7 +98,8 @@ func partition[T any](slice []T, lowIndex, highIndex int, comparator lancetconst
|
|||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
|
|
||||||
// HeapSort use heap to sort slice
|
// HeapSort applys the heap sort algorithm to sort the collection, will change the original collection data.
|
||||||
|
// Play: https://go.dev/play/p/u6Iwa1VZS_f
|
||||||
func HeapSort[T any](slice []T, comparator lancetconstraints.Comparator) {
|
func HeapSort[T any](slice []T, comparator lancetconstraints.Comparator) {
|
||||||
size := len(slice)
|
size := len(slice)
|
||||||
|
|
||||||
@@ -126,7 +132,8 @@ func sift[T any](slice []T, lowIndex, highIndex int, comparator lancetconstraint
|
|||||||
slice[i] = temp
|
slice[i] = temp
|
||||||
}
|
}
|
||||||
|
|
||||||
// MergeSort merge sorting for slice
|
// MergeSort applys the merge sort algorithm to sort the collection, will change the original collection data.
|
||||||
|
// Play: https://go.dev/play/p/ydinn9YzUJn
|
||||||
func MergeSort[T any](slice []T, comparator lancetconstraints.Comparator) {
|
func MergeSort[T any](slice []T, comparator lancetconstraints.Comparator) {
|
||||||
mergeSort(slice, 0, len(slice)-1, comparator)
|
mergeSort(slice, 0, len(slice)-1, comparator)
|
||||||
}
|
}
|
||||||
@@ -167,7 +174,8 @@ func merge[T any](slice []T, lowIndex, midIndex, highIndex int, comparator lance
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// CountSort use count sorting for slice
|
// CountSort applys the count sort algorithm to sort the collection, don't change the original collection data.
|
||||||
|
// Play: https://go.dev/play/p/tB-Umgm0DrP
|
||||||
func CountSort[T any](slice []T, comparator lancetconstraints.Comparator) []T {
|
func CountSort[T any](slice []T, comparator lancetconstraints.Comparator) []T {
|
||||||
size := len(slice)
|
size := len(slice)
|
||||||
out := make([]T, size)
|
out := make([]T, size)
|
||||||
|
|||||||
93
algorithm/sort_example_test.go
Normal file
93
algorithm/sort_example_test.go
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
package algorithm
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func ExampleBubbleSort() {
|
||||||
|
numbers := []int{2, 1, 5, 3, 6, 4}
|
||||||
|
comparator := &intComparator{}
|
||||||
|
|
||||||
|
BubbleSort(numbers, comparator)
|
||||||
|
|
||||||
|
fmt.Println(numbers)
|
||||||
|
// Output:
|
||||||
|
// [1 2 3 4 5 6]
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleCountSort() {
|
||||||
|
numbers := []int{2, 1, 5, 3, 6, 4}
|
||||||
|
comparator := &intComparator{}
|
||||||
|
|
||||||
|
sortedNumber := CountSort(numbers, comparator)
|
||||||
|
|
||||||
|
fmt.Println(numbers)
|
||||||
|
fmt.Println(sortedNumber)
|
||||||
|
// Output:
|
||||||
|
// [2 1 5 3 6 4]
|
||||||
|
// [1 2 3 4 5 6]
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleHeapSort() {
|
||||||
|
numbers := []int{2, 1, 5, 3, 6, 4}
|
||||||
|
comparator := &intComparator{}
|
||||||
|
|
||||||
|
HeapSort(numbers, comparator)
|
||||||
|
|
||||||
|
fmt.Println(numbers)
|
||||||
|
// Output:
|
||||||
|
// [1 2 3 4 5 6]
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleMergeSort() {
|
||||||
|
numbers := []int{2, 1, 5, 3, 6, 4}
|
||||||
|
comparator := &intComparator{}
|
||||||
|
|
||||||
|
MergeSort(numbers, comparator)
|
||||||
|
|
||||||
|
fmt.Println(numbers)
|
||||||
|
// Output:
|
||||||
|
// [1 2 3 4 5 6]
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleInsertionSort() {
|
||||||
|
numbers := []int{2, 1, 5, 3, 6, 4}
|
||||||
|
comparator := &intComparator{}
|
||||||
|
|
||||||
|
InsertionSort(numbers, comparator)
|
||||||
|
|
||||||
|
fmt.Println(numbers)
|
||||||
|
// Output:
|
||||||
|
// [1 2 3 4 5 6]
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleSelectionSort() {
|
||||||
|
numbers := []int{2, 1, 5, 3, 6, 4}
|
||||||
|
comparator := &intComparator{}
|
||||||
|
|
||||||
|
SelectionSort(numbers, comparator)
|
||||||
|
|
||||||
|
fmt.Println(numbers)
|
||||||
|
// Output:
|
||||||
|
// [1 2 3 4 5 6]
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleShellSort() {
|
||||||
|
numbers := []int{2, 1, 5, 3, 6, 4}
|
||||||
|
comparator := &intComparator{}
|
||||||
|
|
||||||
|
ShellSort(numbers, comparator)
|
||||||
|
|
||||||
|
fmt.Println(numbers)
|
||||||
|
// Output:
|
||||||
|
// [1 2 3 4 5 6]
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleQuickSort() {
|
||||||
|
numbers := []int{2, 1, 5, 3, 6, 4}
|
||||||
|
comparator := &intComparator{}
|
||||||
|
|
||||||
|
QuickSort(numbers, comparator)
|
||||||
|
|
||||||
|
fmt.Println(numbers)
|
||||||
|
// Output:
|
||||||
|
// [1 2 3 4 5 6]
|
||||||
|
}
|
||||||
@@ -66,6 +66,7 @@ func TestBubbleSortForStructSlice(t *testing.T) {
|
|||||||
|
|
||||||
func TestBubbleSortForIntSlice(t *testing.T) {
|
func TestBubbleSortForIntSlice(t *testing.T) {
|
||||||
asssert := internal.NewAssert(t, "TestBubbleSortForIntSlice")
|
asssert := internal.NewAssert(t, "TestBubbleSortForIntSlice")
|
||||||
|
|
||||||
numbers := []int{2, 1, 5, 3, 6, 4}
|
numbers := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
BubbleSort(numbers, comparator)
|
BubbleSort(numbers, comparator)
|
||||||
|
|||||||
Reference in New Issue
Block a user