-
-
Save NaveenDA/3f71583594a2795c838ab4e044c09d35 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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