Skip to content

Instantly share code, notes, and snippets.

@cedricconol
Created May 6, 2020 04:13
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 cedricconol/c598b7fd67f3ee0a17894c5ae8b1425b to your computer and use it in GitHub Desktop.
Save cedricconol/c598b7fd67f3ee0a17894c5ae8b1425b to your computer and use it in GitHub Desktop.
optimized swap in bubble sort
func pass(list []int, ascending bool) int {
var N int = len(list)
var swapCount int
for i := 0; i < N-1; i++ {
var firstElement int = list[i]
var secondElement int = list[i+1]
if ascending == true {
if firstElement > secondElement {
swap(list, i)
swapCount++
}
} else {
if firstElement < secondElement {
swap(list, i)
swapCount++
}
}
}
return swapCount
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment