In the previous post, we discussed sorting slices in golang. In this tutorial, we will walk through how to sort an array of ints in Golang.
array
: In computer science, an array is a data structure consisting of a collection of elements (values or variables), each identified by at least one array index or key. An array is stored such that the position of each element can be computed from its index tuple by a mathematical formula. The simplest type of data structure is a linear array, also called a one-dimensional array.
Example 1: Convert to int slice and then use the Ints() function
func Ints(x []int)
: Ints sorts a slice of ints in increasing order.
The below example demonstrates a simple way to sort an int array with Ints()
function:
package main
import (
"fmt"
"reflect"
"sort"
)
func main() {
var arr [5]int
arr[0] = 5
arr[1] = 6
arr[2] = 2
arr[3] = 7
arr[4] = 1
fmt.Println("Before sort: ")
fmt.Println(arr)
fmt.Println("array = ", reflect.TypeOf(arr)) // check type of variable
sort.Ints(arr[:]) // convert array to slice and sort the array
fmt.Println("After sort: ")
fmt.Println(arr)
fmt.Println("array = ", reflect.TypeOf(arr))
}
Output:
Before sort:
[5 6 2 7 1]
array = [5]int
After sort:
[1 2 5 6 7]
array = [5]int
Example 2: Using Slice() function to sort int array in ascending order
In example 2, we will use Slice()
function to sort an int array in ascending order:
func Slice(x any, less func(i, j int) bool)
: Slice sorts the slice x given the provided less function. It panics if x is not a slice. The sort is not guaranteed to be stable: equal elements may be reversed from their original order. For a stable sort, use SliceStable. The less function must satisfy the same requirements as the Interface type's Less method.
package main
import (
"fmt"
"reflect"
"sort"
)
func main() {
var arr [5]int
arr[0] = 5
arr[1] = 6
arr[2] = 2
arr[3] = 7
arr[4] = 1
fmt.Println("Before sort: ")
fmt.Println(arr)
fmt.Println("array = ", reflect.TypeOf(arr)) // check type of variable
sort.Slice(arr[:], func(i, j int) bool {
return arr[:][i] > arr[:][j]
})
fmt.Println("After sort: ")
fmt.Println(arr)
fmt.Println("array = ", reflect.TypeOf(arr))
}
Output:
Before sort:
[5 6 2 7 1]
array = [5]int
After sort:
[7 6 5 2 1]
array = [5]int
Example 3: Write function to do Bubble Sort an array
Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the input list element by element, comparing the current element with the one after it, swapping their values if needed. These passes through the list are repeated until no swaps had to be performed during a pass, meaning that the list has become fully sorted. The algorithm, which is a comparison sort, is named for the way the larger elements "bubble" up to the top of the list.
- Step 1: Iterate the array from 0th index to n-1.
- Step 2: Iterate the array from the 0th index to n-1-i, where i is the index of the above loop.
- Step 3: Swap if the highest element is at the starting position of an array, else leave.
- Step 3: At the end, return the array.
Time Complexity:Â O(n2)
Here is an example of implementing bubble sort an array in Go:
package main
import "fmt"
func main() {
var arr [5]int
arr[0] = 5
arr[1] = 6
arr[2] = 2
arr[3] = 7
arr[4] = 1
fmt.Println("Before sort: ")
fmt.Println(arr)
for i := 0; i <= len(arr)-1; i++ {
for j := 0; j < len(arr)-1-i; j++ {
if arr[j] > arr[j+1] {
arr[j], arr[j+1] = arr[j+1], arr[j]
}
}
}
fmt.Println("After sort: ")
fmt.Println(arr)
}
Output:
Before sort:
[5 6 2 7 1]
After sort:
[1 2 5 6 7]
Summary
In this article I have given examples of sorting elements in an array. We can use built-in function in sort
package or write our own function. Hopefully with the idea introduced, you can freely develop different sort int array applications
References
https://en.wikipedia.org/wiki/Array_(data_structure)
https://en.wikipedia.org/wiki/Bubble_sort
https://pkg.go.dev/sort