Skip to content

Instantly share code, notes, and snippets.

@somersbmatthews
Created January 22, 2021 17:41
Show Gist options
  • Save somersbmatthews/35ef2f82b168fd6b75d2194fdaf1ec6d to your computer and use it in GitHub Desktop.
Save somersbmatthews/35ef2f82b168fd6b75d2194fdaf1ec6d to your computer and use it in GitHub Desktop.
bubble sorts #golang #go #algorithms
// ascending, non-decreasing
func BubbleSort(data []int) {
for i := 0; i < len(data); i++ {
for j := 1; j < len(data)-i; j++ {
if data[j] < data[j-1] {
data[j], data[j-1] = data[j-1], data[j]
}
}
}
}
// descending, non-increasing
for index := 0; index < len(nums)-1; index++ {
for innerIndex := 0; innerIndex < len(nums) - index - 1; innerIndex++ {
if nums[innerIndex] < nums[innerIndex+1] {
nums[innerIndex], nums[innerIndex+1] = nums[innerIndex+1], nums[innerIndex]
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment