mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-13 17:22:27 +08:00
refactor: change function sign for QuickSort
This commit is contained in:
@@ -65,11 +65,15 @@ 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
|
||||||
func QuickSort[T any](slice []T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) {
|
func QuickSort[T any](slice []T, comparator lancetconstraints.Comparator) {
|
||||||
|
quickSort(slice, 0, len(slice)-1, comparator)
|
||||||
|
}
|
||||||
|
|
||||||
|
func quickSort[T any](slice []T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) {
|
||||||
if lowIndex < highIndex {
|
if lowIndex < highIndex {
|
||||||
p := partition(slice, lowIndex, highIndex, comparator)
|
p := partition(slice, lowIndex, highIndex, comparator)
|
||||||
QuickSort(slice, lowIndex, p-1, comparator)
|
quickSort(slice, lowIndex, p-1, comparator)
|
||||||
QuickSort(slice, p+1, highIndex, comparator)
|
quickSort(slice, p+1, highIndex, comparator)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -141,7 +141,7 @@ func TestQuickSort(t *testing.T) {
|
|||||||
{Name: "e", Age: 28},
|
{Name: "e", Age: 28},
|
||||||
}
|
}
|
||||||
comparator := &peopleAgeComparator{}
|
comparator := &peopleAgeComparator{}
|
||||||
QuickSort(peoples, 0, len(peoples)-1, comparator)
|
QuickSort(peoples, comparator)
|
||||||
|
|
||||||
expected := "[{d 8} {b 10} {c 17} {a 20} {e 28}]"
|
expected := "[{d 8} {b 10} {c 17} {a 20} {e 28}]"
|
||||||
actual := fmt.Sprintf("%v", peoples)
|
actual := fmt.Sprintf("%v", peoples)
|
||||||
|
|||||||
@@ -248,7 +248,7 @@ func main() {
|
|||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func QuickSort[T any](slice []T, lowIndex, highIndex int, comparator lancetconstraints.Comparator)
|
func QuickSort[T any](slice []T comparator lancetconstraints.Comparator)
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
@@ -278,7 +278,7 @@ func main() {
|
|||||||
|
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
intSlice := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
algorithm.QuickSort(intSlice, 0, len(intSlice)-1, comparator)
|
algorithm.QuickSort(intSlice, comparator)
|
||||||
|
|
||||||
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
|
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -249,7 +249,7 @@ func main() {
|
|||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func QuickSort[T any](slice []T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) []T
|
func QuickSort[T any](slice []T, comparator lancetconstraints.Comparator) []T
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
@@ -279,7 +279,7 @@ func main() {
|
|||||||
|
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
intSlice := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
algorithm.QuickSort(intSlice, 0, len(intSlice)-1, comparator)
|
algorithm.QuickSort(intSlice, comparator)
|
||||||
|
|
||||||
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
|
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user