为了更好地理解这个示例,我们始终建议您学习下面列出的 Golang 编程 的基础主题
这里我们解释如何在 GO 程序中对数组中的元素列表进行排序。有不同类型的排序算法。在这种情况下,我们使用快速排序来对数组元素进行排序。快速排序是最有效的排序算法之一,常用于生产环境中的排序实现。顾名思义,我们可以说它是最快的排序算法,但您应该注意和小心排序实现,否则您的速度会迅速下降。所以它不适用于大型数据集。
它基于分治算法的原理工作。在这里,它将一个 数组 分成更小的块,这将帮助您快速轻松地排序元素。
这里我们使用一个指定的值,称为支点。一个大的未排序数组根据支点位置被分成两个 子数组。其中一个数组包含小于支点值的值,另一个数组包含大于支点值的值。然后对支点的两侧递归执行排序操作,直到不再有子数组存在。
在这个 GO 程序中,我们使用 'fmt.scanln()' 和 'fmt.println()' 方法接受用户输入的值,这些方法定义在 fmt 包下。为了使用这些函数,我们需要导入“fmt” 包。为了将字符串类型转换为 int 类型,我们使用 Atoi() 函数,它等同于 ParseInt(str string, base int, bitSize int)。您需要在程序中导入“strconv”包才能访问 Atoi() 函数。
这里变量 A 保存数组元素。其他变量 n 和 pivot 分别用作数组的大小和支点索引元素。使用 for 循环读取数组元素。使用两个变量 low 和 high 来存储最低和最高索引位置,除了支点元素。快速排序算法是使用分治法开发的,它属于比较算法类别。
这里我们主要使用两个函数,主 quickSort() 函数和 partition() 函数。partition() 函数用于查找支点值,并负责将所有内容移动到支点的正确一侧。quickSort() 函数用于处理算法的递归性质。
下面是 Go 程序中实现快速排序所使用的步骤。
步骤 1: 导入 'fmt' 和 'strconv' 包。
步骤 2: 打开 main() 开始程序,GO 程序的执行从 main() 开始。
步骤 3:声明变量 n。
步骤 4: 读取数组的大小为 n。
步骤 5: 定义数组 A[]。
步骤 6: 使用 'for 循环' 读取 A[] 数组元素。
步骤 7: 调用函数 quickSort(arr, 0, len(arr) - 1) 执行快速排序。
步骤 8: 使用 for 循环显示排序后的数组。
步骤 9: 退出。
步骤 1: 检查 low < high,然后通过调用 partition(A, low, high) 找到数组下界的支点索引。
步骤 2: 通过调用 quickSort(A, low, pivot) 应用分治策略对支点位置左侧的数组进行排序。
步骤 3: 通过调用 quickSort(A, pivot + 1, high) 对支点位置右侧的数组进行排序。
步骤 1: 选择最低界元素作为支点值。
步骤 2: 声明两个变量 low 和 high(即指针)来存储最低和最高索引位置,除了支点元素。
步骤 3: 赋值 i=low, j=high。
步骤 4: 使用 for 循环,通过递增 'i' 的索引值将左侧指针向右移动,直到指针 A[i] 的值小于或等于 pivot 元素。
步骤 5: 使用 for 循环,通过递减 'j' 的索引值将右侧指针向左移动,直到指针的值大于 pivot 元素。
步骤 6: 仅当 i 小于 j 时,交换数组中索引 i 和 j 处的值。
步骤 7: 将第 j' 个位置的元素与数组的下界交换,并将支点元素放置到 j 的位置。
步骤 8: 最后返回支点元素的正确索引位置。
package main
import (
"fmt"
"strconv"
)
func quickSort(A []int, low, high int) {
if low < high {
// Find the pivot index of an lower bound of an array
var pivot = partition(A, low, high)
// Apply Divide and conquer strategy
// to sort the left side and right side of an array
// respective to the pivot position
// Left hand side array
quickSort(A, low, pivot)
// Right hand side array
quickSort(A, pivot + 1, high)
}
}
func partition(A []int, low, high int) int {
// Pick a lowest bound element as an pivot value
var pivot = A[low]
var i = low
var j = high
for i < j {
// Increment the index value of "i"
// till the left most values should be less than or equal to the pivot value
for A[i] <= pivot && i < high {
i++;
}
// Decrement the index value of "j"
// till the right most values should be greater than to the pivot value
for A[j] > pivot && j > low {
j--
}
// Interchange the values of present
// in the index i and j of an array only if i is less than j
if i < j {
var temp = A[i]
A[i] = A[j]
A[j] = temp
}
}
// Interchange the element in j's poisition to the lower bound of an array
// and place the Pivot element to the j's position
A[low] = A[j]
A[j] = pivot
// Finally return the appropriate index position of the pivot element
return j
}
func main() {
fmt.Println("Enter the size of the array")
var n int
fmt.Scan(&n)
A := make([]int, n, 100)
fmt.Println("Enter elements : ")
for i := 0; i < n; i++ {
fmt.Scan(&A[i])
}
quickSort(A, 0, len(A) - 1)
fmt.Print("After Sorting: ")
for i := 0; i < n; i++ {
fmt.Print(strconv.Itoa(A[i]) + " ")
}
}
Enter the size of the array 5 Enter elements: 81 60 73 52 95 After Sorting: 52 60 73 81 95