- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Golang Program To Sort An Array In Ascending Order Using Insertion Sort

Insertion sort is defined as an In-Place Algorithm and it is declared as a stable algorithm. The idea of sorting an array in ascending or descending order by swapping an element can be used to implement Insertion Sort. For instance, if the array contains only one item in the list, then it is considered sorted. Otherwise, we consider that a certain portion of the list of elements is sorted while other is unsorted. Proceeding with this assumption, we traverse through the unsorted list, picking one element at a time.

## Syntax

rand.Seed(value)

**Rand.Seed()** function is used to generate random numbers. It takes a user input as argument which is the upper limit for generating random numbers.

func Now() Time

The **Now()** function is defined in time package. this function generates the current local time. to use this function we have to first impot the time package in our program.

func (t Time) UnixNano() int64

The **UnixNano()** function is defined in time package. this function is used to get the number of seconds passed from January 1 1970 in utc. The final result returned by it is of integer type of 64 bits.

rand.Intn(n)

The **Intn()** function is defined in the math/rand package. it is used to generate a random number in the interval from [0, n]. where n is a number provided by user. The function throws an error if the provided number is lesser than 0.

## Algorithm Steps

**Step 1** − If it is the first element, then elements in the list are already sorted.

**Step 2** − If STEP 1 is false i.e list is not sorted, then pick the next element, known as key

**Step 3** − The key value of STEP 2 is compared with all elements in the sorted sub-list

**Step 4** −Elements are shifted in the sorted sub-list that is greater than the key value

**Step 5** − Key value is to be inserted at the appropriate place in the sorted list

**Step 6** − Repeat each step until the list is sorted

## Example

Sorting an integer array in ascending order using insertion sort.

package main import "fmt" func main() { arr := [6]int{5, 7, 3, 4, 1, 2} var flag int = 0 var item int = 0 // printing the array fmt.Println("The array entered by the user is:\n", arr) //Insertion Sort to arrange elements in ascending order for i := 0; i < 6; i++ { item = arr[i] flag = 0 for j := i - 1; j >= 0 && flag != 1; j-- { if item < arr[j] { arr[j+1] = arr[j] j = j - 1 arr[j+1] = item } else { flag = 1 } } } // printing new line fmt.Println() // printing the result fmt.Println("Array after sorting is: \n", arr) }

## Output

The array entered by the user is: [5 7 3 4 1 2] Array after sorting is: [2 4 2 5 2 7]

## Problem Solution

In the above program, we will read an array of elements from the user then sort the array in an ascending order using insertion sort and then print the sorted array on the output screen.

## Explanation

In the above example, we have declared the main package. This main package is used to indicate compiler that the package will compile and then produce executable files.

We have also imported fmt package. fmt stands for Format Package. This package is used to format basic strings and values. It will serve the purpose to include the package fmt, which enables us to use functions related to fmt.

## Example

Sorting an integer array in ascending order using insertion sort with randomized values.

package main import ( "fmt" "math/rand" // math/rand package provides pseudorandom number generation "time" // time package provides functionality for measuring and displaying time ) func main() { // calling the generateSlice() function slice := generateSlice(20) fmt.Println("Unsorted Numbers are:\n", slice) insertionsort(slice) fmt.Println("Sorted Numbers are:\n", slice, "\n") } // Generates a slice of size, size filled with random numbers func generateSlice(size int) []int { slice := make([]int, size, size) // generating a random number from the seconds passed from 1 january 1970 to current time. rand.Seed(time.Now().UnixNano()) for i := 0; i < size; i++ { slice[i] = rand.Intn(100) - rand.Intn(100) } return slice } func insertionsort(items []int) { var n = len(items) for i := 1; i < n; i++ { j := i for j > 0 { if items[j-1] > items[j] { items[j-1], items[j] = items[j], items[j-1] } j = j - 1 } } }

## Output

Unsorted Number [-28 -35 -26 -3 4 27 14 -30 26 50 -32 2 68 15 -7 10 -1 60 7 -62] Sorted Number [-62 -35 -32 -30 -28 -26 -7 -3 -1 2 4 7 10 14 15 26 27 50 60 68]

## Explanation

In the above program, we have used rand.Seed() to generate random numbers It is used to set a seed value to generate pseudo-random numbers. We need to sort them in ascending order by implementation Insertion sort. Slice in the above program is used as a flexible and extensible data structure to implement and manage collections of data.

## Conclusion

In the above tutorial, we have understood how to sort an array in ascending order using insertion sort in 2 different examples, given that one array is completely positive in the first example and the second array has negative elements in it.

- Related Articles
- Swift Program to sort an array in ascending order using insertion sort
- Golang Program to sort an array in descending order using insertion sort
- Swift Program to sort an array in descending order using insertion sort
- Swift Program to sort an array in ascending order using bubble sort
- Swift Program to sort an array in ascending order using selection sort
- Swift Program to sort an array in ascending order using quick sort
- Golang Program To Sort The Elements Of An Array In Ascending Order
- C program to sort an array in an ascending order
- 8086 program to sort an integer array in ascending order
- Java Program to Sort Array list in an Ascending Order
- Implementing insertion sort to sort array of numbers in increasing order using JavaScript
- Python program to sort the elements of an array in ascending order
- C++ Program to Sort the Elements of an Array in Ascending Order
- Swift Program to Sort the Elements of an Array in Ascending Order
- C program to sort an array of ten elements in an ascending order