Friday, November 22, 2024
Google search engine
HomeLanguagesGolangHow to sort a slice of ints in Golang?

How to sort a slice of ints in Golang?

In Go language slice is more powerful, flexible, convenient than an array, and is a lightweight data structure. The slice is a variable-length sequence which stores elements of a similar type, you are not allowed to store different type of elements in the same slice. Go language allows you to sort the elements of the slice according to its type. So, an int type slice is sorted by using the following functions. These functions are defined under the sort package so, you have to import sort package in your program for accessing these functions: 1. Ints: This function is used to only sorts a slice of ints and it sorts the elements of the slice in increasing order. Syntax: 

func Ints(slc []int)

In Go, you can sort a slice of ints using the sort package. The sort package provides several sorting algorithms for various data types, including int and []int.

To sort a slice of ints in Go, you can use the sort.Ints function, which sorts the slice in place and returns no value. Here’s an example that demonstrates how to sort a slice of ints in Go:

Go




package main
 
import (
    "fmt"
    "sort"
)
 
func main() {
    intSlice := []int{5, 2, 6, 3, 1, 4}
    sort.Ints(intSlice)
    fmt.Println(intSlice) // [1 2 3 4 5 6]
}


Output:

[1 2 3 4 5 6]
 

In this example, the slice intSlice is sorted in ascending order by the sort.Ints function, and the sorted slice is then printed to the console.

Note that the sort.Ints function sorts the slice in place, meaning that it modifies the original slice and returns no value. If you need to preserve the original slice and return a sorted copy, you can use the copy function to make a copy of the original slice, and then sort the copy.

Here, slc represent a slice of ints. Let us discuss this concept with the help of an example: 

Example: 

C




// Go program to illustrate how
// to sort the slice of ints
package main
  
import (
    "fmt"
    "sort"
)
  
// Main function
func main() {
      
    // Creating and initializing slices
    // Using shorthand declaration
    scl1 := []int{400, 600, 100, 300, 500, 200, 900}
    scl2 := []int{-23, 567, -34, 67, 0, 12, -5}
      
    // Displaying slices
    fmt.Println("Slices(Before):")
    fmt.Println("Slice 1: ", scl1)
    fmt.Println("Slice 2: ", scl2)
      
    // Sorting the slice of ints
    // Using Ints function
    sort.Ints (scl1)
    sort.Ints (scl2)
      
    // Displaying the result
    fmt.Println("\nSlices(After):")
    fmt.Println("Slice 1 : ", scl1)
    fmt.Println("Slice 2 : ",scl2)
}


Output:

Slices(Before):
Slice 1:  [400 600 100 300 500 200 900]
Slice 2:  [-23 567 -34 67 0 12 -5]

Slices(After):
Slice 1 :  [100 200 300 400 500 600 900]
Slice 2 :  [-34 -23 -5 0 12 67 567]

2. IntsAreSorted: This function is used to check whether the given slice of ints is in the sorted form(in increasing order) or not. This method returns true if the slice is in sorted form, or return false if the slice is not in the sorted form. Syntax:

func IntsAreSorted(scl []int) bool

Here, scl represents a slice of ints. Let us discuss this concept with the help of an example: Example: 

C




// Go program to illustrate how to check
// whether the given slice of ints is in
// sorted form or not
package main
 
import (
    "fmt"
    "sort"
)
 
// Main function
func main() {
 
    // Creating and initializing slices
    // Using shorthand declaration
    scl1 := []int{100, 200, 300, 400, 500, 600, 700}
    scl2 := []int{-23, 567, -34, 67, 0, 12, -5}
 
    // Displaying slices
    fmt.Println("Slices:")
    fmt.Println("Slice 1: ", scl1)
    fmt.Println("Slice 2: ", scl2)
 
    // Checking the slice is in sorted form or not
    // Using IntsAreSorted function
    res1 := sort.IntsAreSorted(scl1)
    res2 := sort.IntsAreSorted(scl2)
 
    // Displaying the result
    fmt.Println("\nResult:")
    fmt.Println("Is Slice 1 is sorted?: ", res1)
    fmt.Println("Is Slice 2 is sorted?: ", res2)
}


Output:

Slices:
Slice 1:  [100 200 300 400 500 600 700]
Slice 2:  [-23 567 -34 67 0 12 -5]

Result:
Is Slice 1 is sorted?:  true
Is Slice 2 is sorted?:  false

RELATED ARTICLES

Most Popular

Recent Comments