Check if the given slice is sorted in Golang


In Golang, it is important to know whether a slice is sorted or not, especially when working with algorithms that require sorted data. In this article, we will explore various methods to check if a given slice is sorted or not.

Using a Loop to Check if the Slice is Sorted

One way to check if a slice is sorted is to use a loop to compare adjacent elements in the slice. If the elements are in ascending order, then the slice is sorted. Here is an example code −

Example

package main

import "fmt"

func isSorted(s []int) bool {
   n := len(s)
   for i := 1; i < n; i++ {
      if s[i] < s[i-1] {
         return false
      }
   }
   return true
}

func main() {
   s1 := []int{1, 2, 3, 4, 5}
   s2 := []int{1, 3, 2, 4, 5}
   fmt.Println(isSorted(s1)) // true
   fmt.Println(isSorted(s2)) // false
}

Output

true
false

In this example, we define the isSorted function, which takes an integer slice and returns a boolean result indicating whether the slice is sorted or not. In order to compare neighbouring elements in the slice, the function employs a loop. The function returns false if it is determined that an element is smaller than the one before it. If not, it gives back true.

Using the sort.SliceIsSorted Function

Golang provides a built-in function called sort.SliceIsSorted that can be used to check if a slice is sorted. This function takes a slice and a comparison function as arguments and returns a boolean value indicating whether the slice is sorted or not. Here is an example code −

Example

package main

import (
   "fmt"
   "sort"
)

func main() {
   s1 := []int{1, 2, 3, 4, 5}
   s2 := []int{1, 3, 2, 4, 5}

   fmt.Println(sort.SliceIsSorted(s1, func(i, j int) bool {
      return s1[i] < s1[j]
   })) // true

   fmt.Println(sort.SliceIsSorted(s2, func(i, j int) bool {
      return s2[i] < s2[j]
   })) // false
}

Output

true
false

In this example, we use the sort.SliceIsSorted function to check if the two slices are sorted or not. The function takes a slice and a comparison function as arguments. The comparison function returns true if the element at index i is less than the element at index j. The function returns true if the slice is sorted and false otherwise.

Using the sort.IntsAreSorted Function

If you are working with a slice of integers, you can use the sort.IntsAreSorted function to check if the slice is sorted. This function takes a slice of integers as an argument and returns a boolean value indicating whether the slice is sorted or not. Here is an example code −

Example

package main

import (
   "fmt"
   "sort"
)

func main() {
   s1 := []int{1, 2, 3, 4, 5}
   s2 := []int{1, 3, 2, 4, 5}

   fmt.Println(sort.IntsAreSorted(s1)) // true
   fmt.Println(sort.IntsAreSorted(s2)) // false
}

Output

true
false

In this example, we use the sort.IntsAreSorted function to check if the two slices are sorted or not. The function returns true if the slice is sorted in ascending order and false

Conclusion

Checking if a slice is sorted or not is an important task in Golang. In this article, we explored various methods to check if a given slice is sorted or not. We used a loop to compare adjacent elements in the slice, the sort.SliceIsSorted function, and the sort.IntsAreSorted function to check if a slice is sorted or not. By using these methods, we can easily check if a slice is sorted and take necessary actions accordingly.

Updated on: 07-Apr-2023

259 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements