Golang Program to sort an array in descending order using insertion sort


Insertion sort is a simple sorting algorithm that works similarly to the way you sort playing cards in your hands. The array is virtually split into a sorted and an unsorted part. The elements from unsorted arrays are picked and placed at the correct positions in unsorted arrays, as a result, the array becomes sorted. Here we are going to learn different approaches to sorting an array in descending order using insertion sort in go programming language.

Syntax

func append(slice, element_1, element_2…, element_N) []T

The append function is used to add values to an array slice. It takes number of arguments. The first argument is the array to which we wish to add the values followed by the values to add. The function then returns the final slice of array containing all the values.

func make ([] type, size, capacity)

The make function in go language is used to create an array/map it accepts the type of variable to be created, its size and capacity as arguments and returns the slice which we can store in the variable.

Algorithm

  • Step 1 − First, we need to import the fmt package.

  • Step 2 − Then, we need to start the main() function. Inside this function initialize an array of integers by using make() function and assign values to it by using append() function.

  • Step 3 − Then, we need to print the array on the screen using fmt.Println() function. Then use a for loop to iterate over the array and store the current element of the array in a temporary variable called key. Initialize a new variable and assign value to it by decrementing the index.

  • Step 4 − Now, use a for loop and check whether the current element is greater than the previous element.

  • Step 5 − If the condition is satisfied then swap the two values and decrement the current index in this inner loop.

  • Step 6 − Repeat the process until the whole array is iterated upon and print the final array thus achieved on the screen.

  • Step 7 − Now, start the main() function initialize an array, and store values to it. print the array on the screen.(For example 2)

  • Step 8 − Now, call the insertionSort() function by passing the array as an argument and store the result in a new array called result. (For Example 2)

  • Step 9 − Print the array obtained on the screen.

Example 1

In this example, we will write a go language program to sort the array of integers in descending order by using the insertion sort.

package main
import "fmt"
func main() {
   // initializing an array
   arr := make([]int, 0, 5)
   arr = append(arr, 5, 2, 4, 6, 1, 3)
   fmt.Println("The given array is: ", arr)
   for i := 1; i < len(arr); i++ {
      key := arr[i]
      j := i - 1
      for j >= 0 && arr[j] < key {
         arr[j+1] = arr[j]
         j = j - 1
      }
      arr[j+1] = key
   }
   fmt.Println("Sorted Array in Descending Order: ", arr)
}

Output

The given array is:  [5 2 4 6 1 3]
Sorted Array in Descending Order:  [6 5 4 3 2 1]

Example 2

In this example, we will show how we can use an external user defined function to sort an array in descending order using insertion sort. The function that we create accepts the array to be sorted as an argument and returns the final sorted array as the result that we can print on the screen.

package main
import "fmt"
func insertionSort(arr []int) []int {
   for i := 1; i < len(arr); i++ {
      key := arr[i]
      j := i - 1
      for j >= 0 && arr[j] < key {
         arr[j+1] = arr[j]
         j = j - 1
      }
      arr[j+1] = key
   }
   return arr
}

func main() {
   // initializing an array
   arr := make([]int, 0, 5)
   arr = append(arr, 5, 2, 4, 6, 1, 3)
   fmt.Println("The given array is: ", arr)
   res := insertionSort(arr)
   fmt.Println("Sorted Array in Descending Order:", res)
}

Output

The given array is:  [5 2 4 6 1 3]
Sorted Array in Descending Order: [6 5 4 3 2 1]

Conclusion

We have successfully compiled and executed a go language program to sort an array in descending order using insertion sort along with examples. we have used two examples here one using the main() section of the program and other using the external function.

Updated on: 16-Feb-2023

506 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements