Skip to content

Instantly share code, notes, and snippets.

@NaveenDA
Last active June 28, 2021 04:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NaveenDA/3f71583594a2795c838ab4e044c09d35 to your computer and use it in GitHub Desktop.
Save NaveenDA/3f71583594a2795c838ab4e044c09d35 to your computer and use it in GitHub Desktop.
package main
import "fmt"
func main() {
// declare an array
items := [6]int{5, 2, 4, 6, 1, 3}
// calculate length of array
var n = len(items)
// loop through array
for i := 1; i < n; i++ {
// set limit for current index (for leftside shift)
j := i
// loop through array until we reach the last element
for j > 0 {
// If the Item from right side is greater than left side
// we need to swap the values
if items[j-1] > items[j] {
items[j-1], items[j] = items[j], items[j-1]
}
/*
* else {
* no need to swap, because this unit already sortted
* }*/
// reduce inner index by one
j = j - 1
}
}
fmt.Println(items)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment