Your bubble sort contains an error. The output is [1 2 9 7 39 11 54] which is clearly not fully sorted. It only does one iteration. Just need to add an isDone = false inside of the if statement
[1 2 9 7 39 11 54]
isDone = false
package main import "fmt" func main() { var n = []int{1, 39, 2, 9, 7, 54, 11} var isDone = false for !isDone { isDone = true var i = 0 for i < len(n)-1 { if n[i] > n[i+1] { n[i], n[i+1] = n[i+1], n[i] isDone = false } i++ } } fmt.Println(n) }
Thank you so much for spotting the error. Don't know how missed that. :)
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Your bubble sort contains an error. The output is
[1 2 9 7 39 11 54]
which is clearly not fully sorted. It only does one iteration. Just need to add anisDone = false
inside of the if statementThank you so much for spotting the error. Don't know how missed that. :)