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 語言的資訊,您可以瀏覽這些教程。