Go语言实现线性查找算法
在编程中,要从数组、链表或任何其他数据结构中搜索任何内容,我们有一些搜索算法,其中一种是线性搜索。在线性搜索中,我们从起始位置迭代数据结构,并搜索到最后一个索引的元素。
线性搜索算法的优点是可以对已排序和未排序的数据执行此搜索。缺点是,对于已排序或未排序的两种数据,查找元素所需的时间相同。
例如,我们有一个元素数组 {10, 5, 3, 7, 6, 12},我们必须找到元素 3,然后在线性搜索算法中,我们将
首先,与索引 0 处的 10 进行比较,因为值不相等,所以移动到下一个索引。
现在,我们将 5 与 3 进行比较,因为两者不相等,所以移动到下一个索引。
现在我们在索引 2 处找到了元素。
算法
步骤 1:使用 import 关键字在顶部导入所需的包。
步骤 2:然后主函数将首先运行。
首先,我们声明并初始化数组。
调用 linearSearch() 函数通过传递数组、大小和要搜索的元素作为参数来搜索元素。
最后,打印结果,说明元素是否存在。
步骤 3.1:在 linearSearch() 函数中
在第一种方法中,我们正在对数组运行 for 循环。
在循环内部,我们将元素与当前索引处的值进行比较。如果值匹配,则返回索引。
最后,如果元素不存在,则返回 -1。
步骤 3.2:在 linearSearch() 函数中
在第二种方法中,该函数是递归的。
基本条件是当前索引必须小于数组的大小,否则返回 -1。
如果当前索引处的值等于我们正在查找的值,则返回索引。
示例 1
在这个例子中,我们将通过从数组的 0 索引运行 for 循环到最后一个索引来实现线性搜索。在每个索引上,我们将当前索引元素与我们正在搜索的元素进行比较,如果两个元素匹配,则返回索引。
package main
import "fmt"
// function to print the array with array and
// size of the array as argument
func printArray(array []int, size int) {
for i := 0; i < size; i++ {
fmt.Print(array[i], " ")
}
fmt.Println()
}
// linear function to find an element in the array
func linearSearch(array []int, size int, toFind int) int {
// running for loop over the array
for i := 0; i < size; i++ {
//Comparing the current index value with the
// value we want to find
if array[i] == toFind {
return i
}
}
// returning -1 if value not present in the array
return -1
}
func main() {
// declaring and initializing the
// array using the shorthand method
array := []int{10, 5, 3, 7, 6, 12}
// declaring and initializing the
// variable using the var keyword
var toSearch int
toSearch = 3
fmt.Println("Golang program to find an element in an array using linear search.")
fmt.Print("array:")
printArray(array, 6)
// linear search function passing array and
// variable as a parameter
index := linearSearch(array, 6, toSearch)
if index == -1 {
fmt.Println(toSearch, "is not present in the array.")
} else {
fmt.Println(toSearch, "is present at index 2 in the array.")
}
}
输出
元素存在。
Golang program to find an element in an array using linear search. array: 10 5 3 7 6 12 3 is present at index 2 in the array.
示例 2
在这个例子中,我们将使用递归方法实现线性搜索。将会有递归调用,我们将在每次函数调用中增加索引。在每次函数调用中,我们将当前索引元素与我们正在搜索的元素进行比较,如果两个元素匹配,则返回索引。
package main
import "fmt"
// function to print the array with array and
// size of the array as argument
func printArray(array []int, size int) {
for i := 0; i < size; i++ {
fmt.Print(array[i], " ")
}
fmt.Println()
}
// linear function to find an element in the array
func linearSearch(array []int, currIndex int, size int, toFind int) int {
if currIndex >= size {
// returning -1 if value not present in the array
return -1
}
//Comparing the current index value with the
// value we want to find
if array[currIndex] == toFind {
return currIndex
}
// calling linearSearch function for the next index
return linearSearch(array, currIndex+1, size, toFind)
}
func main() {
// declaring and initializing the
// array using the shorthand method
array := []int{10, 5, 3, 7, 6, 12}
// declaring and initializing the
// variable using the var keyword
var toSearch int
toSearch = 3
fmt.Println("Golang program to find an element in an array using linear search recursively.")
fmt.Print("array:")
printArray(array, 6)
// linear search function passing array and
// variable as a parameter
index := linearSearch(array, 0, 6, toSearch)
if index == -1 {
fmt.Println(toSearch, "is not present in the array.")
} else {
fmt.Println(toSearch, "is present at index 2 in the array.")
}
}
输出
Golang program to find an element in an array using linear search recursively. array: 10 5 3 7 6 12 3 is present at index 2 in the array.
结论
这是在 Go 语言中执行线性搜索的两种方法。第二种方法有多个函数调用,对于具有大量元素的数组,堆栈将过载。对于简单的操作这样做不是一种合适的编程方式,因此运行 for 循环的第一种方法将是实现此目标的更有效方法。要了解更多关于 Go 语言的信息,您可以浏览这些教程。
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP