Go語言程式:排序包含0、1和2的陣列
在Go語言中,與其他程式語言一樣,我們可以編寫邏輯來對包含0、1和2作為元素的陣列進行排序。排序意味著按升序或降序排列資料。這是面試中關於陣列的常見問題之一。我們可以採用兩種方法來實現這一點,我們將逐一探討。
例如,我們有一個數組2, 1, 0, 0, 1, 2,排序後陣列將變為0, 0, 1, 1, 2, 2。
方法一
在這個例子中,我們將使用預構建的排序函式將陣列按升序排序。這種方法的時間複雜度為O(nlogn),因為它在後臺使用歸併排序。
演算法
步驟1: 使用`import`關鍵字在頂部匯入所需的包。
步驟2: 然後`main`函式將首先執行。
首先,我們宣告並初始化陣列。
然後我們使用預構建的`sort`函式對陣列進行排序。
最後,我們列印排序後的陣列。
示例
這是使用預構建排序函式在Go語言中對包含0、1和2作為元素的陣列進行排序的程式碼。
package main
import (
"fmt"
"sort"
)
// function to print the array with array and
// size of the array as argument
func printArray(array [5]int, size int) {
for i := 0; i < 5; i++ {
fmt.Print(array[i], " ")
}
fmt.Println()
}
func main() {
// shorthand array declaration
array := [5]int{2, 1, 1, 2, 0}
fmt.Println("Golang program to sort 1s, 2s, and 3s in O(NLog(N)) time complexity.")
fmt.Println("Printing array before sorting.")
// calling function to print the array
printArray(array, 5)
// calling sortArray function to sort the array by
// passing array as reference
sort.Ints(array[:])
fmt.Println("Printing array after sorting.")
// calling function to print the array
printArray(array, 5)
}
輸出
Golang program to sort 1s, 2s, and 3s in O(NLog(N)) time complexity. Printing array before sorting. 2 1 1 2 0 Printing array after sorting. 0 1 1 2 2
方法二
在這個例子中,我們將使用雙指標演算法將陣列按升序排序。這種方法的時間複雜度為O(N),其中N是陣列的大小。
演算法
步驟1: 使用`import`關鍵字在頂部匯入所需的包。
步驟2: 然後`main`函式將首先執行。
首先,我們宣告並初始化陣列。
然後我們呼叫`sortArray()`函式,並將陣列作為引數傳遞,該函式返回排序後的陣列。
最後,我們列印排序後的陣列。
步驟3
在`sortArray()`函式中,我們首先宣告並初始化0、1和2的迭代器。
然後我們執行一個for迴圈,其中每個迭代器都與0、1和2進行比較,並相應地使用`swap`函式進行交換。
示例
這是使用雙指標演算法在Go語言中對包含0、1和2作為元素的陣列進行升序排序的程式碼。
package main
import "fmt"
// function to print the array with array and
// size of the array as argument
func printArray(array [5]int, size int) {
for i := 0; i < 5; i++ {
fmt.Print(array[i], " ")
}
fmt.Println()
}
// function to swap two int values
func swap(a *int, b *int) {
temp := *a
*a = *b
*b = temp
}
// function to sort the array that has values 0s, 1s and 2s
func sortArray(array *[5]int, size int) {
// declaring variables using the var keyword
var i, j, k int
//Initializing the variables
i = 0
j = 0
k = size - 1
// running loop till i is less than j
for j <= k {
if array[j] == 0 {
// if the value at index jth is 0 replace with ith index
swap(&array[i], &array[j])
i++
j++
} else if array[j] == 2 {
// If the value at index jth is 0 replace with ith index
swap(&array[k], &array[j])
k--
} else if array[j] == 1 {
// increasing jth iterator if the value is 1 at the current index
j++
}
}
}
func main() {
// shorthand array declaration
array := [5]int{2, 1, 1, 2, 0}
fmt.Println("Golang program to sort 1s, 2s, and 3s in O(N) time complexity.")
fmt.Println("Printing array before sorting.")
// calling function to print the array
printArray(array, 5)
// calling sortArray function to sort the array by
// passing array as reference
sortArray(&array, 5)
fmt.Println("Printing array after sorting.")
// calling function to print the array
printArray(array, 5)
}
輸出
Golang program to sort 1s, 2s, and 3s in O(N) time complexity. Printing array before sorting. 2 1 1 2 0 Printing array after sorting. 0 1 1 2 2
結論
這是對包含0、1和2作為元素的陣列進行排序的兩種方法。在程式設計中,無論何時使用演算法解決任何問題陳述,時間複雜度都會降低,這在第二種方法中得到了體現,它在時間效率上優於第一種方法,因為它僅使用雙指標演算法花費O(N)的時間。要了解更多關於Go語言的資訊,您可以瀏覽這些教程。
資料結構
網路
關係資料庫管理系統(RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP