# Golang Program to Sort an Array of 0’s, 1’s and 2’s

In Golang, like other programming languages, we can code the logic to sort an array that has 0’s, 1’s, and 2’s as elements. Sorting means assigning the data either in increasing order or in decreasing order. This is one of the famous questions on the array asked in interviews. There can be two approaches to achieve this that we are going to explore one by one.

For example, we have an array 2, 1, 0, 0, 1, 2, and after sorting the array will look like 0, 0, 1, 1, 2, 2.

## Method 1

In this example, we are going to use the pre−build sorting function to sort the array in ascending order. The time complexity of this approach is O(nlogn) as in the background it will use merge sort.

### Algorithm

Step 1: Import the required packages at the top using the import keyword.

Step 2: Then the main function will get run first.

• First, we declare and initialize the array.

• Then we are sorting the array using the pre−built sort function and sorting the array,

• In the end, we are printing the sorted array.

### Example

This is the code for sorting 0’s, 1’s, and 2’s as elements using a pre−build sorting function to sort the array in Golang.

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)
}


### Output

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


## Method 2

In this example, we are going to use the two−pointer algorithm to sort the array in ascending order. The time complexity of this approach is O(N) where N is the size of the array.

### Algorithm

Step 1: Import the required packages at the top using the import keyword.

Step 2: Then the main function will get run first.

• First, we declare and initialize the array.

• Then we are calling sortArray() function in which we are passing an array as a parameter and the function is returning the sorted array.

• In the end, we are printing the sorted array.

Step 3:

• In the sortAraay() function first we declare and initialize the iterators for 0a, 1s, and 2s.

• Then we are running a for loop in which each iterator is compared with the 0s, 1s, and 2s and accordingly, we are swapping using the swap function.

### Example

This is the code for sorting 0’s, 1’s, and 2’s as elements using a two-pointer algorithm to sort the array in ascending order in Golang.

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)
}


### Output

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


## Conclusion

These are the two ways to sort an array that has 0’s, 1’s, and 2’s as elements. In programming, whenever we solve any problem statement using the algorithms the time complexity gets reduced which we are doing in the second one that is efficient in terms of time from the first one as it is taking only O(N) time with the help of a two−pointer algorithm. To learn more about Golang you can explore these tutorials.

Updated on: 10-Jul-2023

36 Views